4-5解法2

是将所有操作保存,然后对于每个查询重新执行每个操作,但不需要计算整
个电子表格的变化,而只需关注所查询的单元格的位置变化。对于题目给定的规模来说,这
个方法不仅更好写,而且效率更高。(来自紫书)

比较简单 直接看代码:

#include <cstdio>
#include <cstring>
#include <iostream>

typedef long long int LL;
const int maxn = 1000;
const int inf = 0x3f3f3f3f;
using namespace std;

struct Command
{
    char c[5];
    int r1,r2,c1,c2;
    int a,x[20];
}cmd[maxn];
int r,c,n;
int simulate(int *r0, int *c0)
{
    for(int i = 0; i < n; i++)
    {
        if(cmd[i].c[0] == 'E')
        {
            if(cmd[i].r1 == *r0 && cmd[i].c1 == *c0)
            {
                *r0 = cmd[i].r2;
                *c0 = cmd[i].c2;
            }
            else if(cmd[i].r2 == *r0 && cmd[i].c2 == *c0)
            {
                *r0 = cmd[i].r1;
                *c0 = cmd[i].c1;
            }
        }
        else
        {
            int dr = 0, dc = 0;
            for(int j = 0; j < cmd[i].a; j++)
            {
                int x = cmd[i].x[j];
                if(cmd[i].c[0] == 'I')
                {
                    if(cmd[i].c[1] == 'R' && x <= *r0) dr++;
                    if(cmd[i].c[1] == 'C' && x <= *c0) dc++;
                }
                else
                {
                    if(cmd[i].c[1] == 'R' && x == *r0) return 0;
                    if(cmd[i].c[1] == 'C' && x == *c0) return 0;
                    if(cmd[i].c[1] == 'R' && x < *r0) dr--;
                    if(cmd[i].c[1] == 'C' && x < *c0) dc--;
                }
            }
            *r0 += dr;
            *c0 += dc;
        }
    }
    return 1;
}
int main(void)
{
    int r0,c0,q,kase = 0;
    while(scanf("%d%d%d",&r,&c,&n) == 3&&r)
    {
        for(int i = 0;i<n; i++ )
        {
            scanf("%s",cmd[i].c);
            if(cmd[i].c[0] == 'E')
            {
                scanf("%d%d%d%d",&cmd[i].r1,&cmd[i].c1,&cmd[i].r2,&cmd[i].c2);
            }
            else
            {
                scanf("%d",&cmd[i].a);
                for(int j = 0;j<cmd[i].a;j++)scanf("%d",&cmd[i].x[j]);
            }
        }
        if(kase>0)printf("\n");
        printf("Spreadsheet #%d\n",++kase);
        scanf("%d",&q);
        while(q--)
        {
            scanf("%d%d",&r0,&c0);
            printf("Cell data in (%d,%d) ",r0,c0);
            if(!simulate(&r0,&c0))printf("GONE");
            else printf("moved to (%d,%d)",r0,c0);
            putchar('\n');
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值