Spreadsheet Tracking UVA - 512(追踪电子表格中的单元格)

题目链接:Spreadsheet Tracking UVA - 512

思路:可以说是一个模拟,将所有的操作先保存起来,然后对于每个查询重新执行每个操作,不需要记录整个表格的变化,只需要关注所查询单元格的位置变化。

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<iomanip>
#define ll long long

using namespace std;
const int maxn = 10000;

struct Command{
    string s;
    int r1,c1,r2,c2;
    int a, x[20];
} cmd[maxn];
int r,c,n;
bool simulate(int &a, int &b) {
    for(int i = 0; i < n; i++) {
     if(cmd[i].s[0] == 'E') {
        if(cmd[i].r1 == a&&cmd[i].c1 == b) { a = cmd[i].r2; b = cmd[i].c2; }
        else if(cmd[i].r2 == a&&cmd[i].c2 == b) { a = cmd[i].r1; b = 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].s[0] == 'I')
            {
                if(cmd[i].s[1] == 'R'&& x <= a) dr++;
                if(cmd[i].s[1] == 'C'&& x <= b) dc++;
            } else {
                if(cmd[i].s[1] == 'R'&& x == a) return false;
                if(cmd[i].s[1] == 'C'&& x == b) return false;
                if(cmd[i].s[1] == 'R'&& x < a) dr--;
                if(cmd[i].s[1] == 'C'&& x < b) dc--;
            }
        }
        a += dr;
        b += dc;
     }
    }
    return true;
}



int main(){
    int r0,c0,p, kase = 0;
    while(1) {
        scanf("%d%d",&r,&c);
        if(r == 0&&c == 0) break;
        scanf("%d",&n);
        for(int i = 0; i < n; i++) {
            cin>>cmd[i].s;
            if(cmd[i].s[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", &p);
        while(p--) {
            scanf("%d%d",&r0,&c0);
            printf("Cell data in (%d,%d) ",r0,c0);
            if(!simulate(r0,c0)) printf("GONE\n");
            else printf("moved to (%d,%d)\n",r0,c0);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值