例题4-5 UVA512 追踪电子表格中的单元格 Spreadsheet Tracking

无语,一开始结构体里面用的不是数组是vector,但是不知道为啥就是过不了,换了数组就过了,现在还不明白。

然后这道题有两种思路,一种就是先模拟完所有的操作,第二种就是先把操作寸起来,输入一个查询模拟一个,第一种还没有写过,直接按第二种思路来写的

这道题的插入删除操作,是一系列同时完成的,不是一个一个做的,

#include <bits/stdc++.h>

#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pa;

struct record {
    string op;
    int v[20], num;
} rec[10005];

int main() {
    int r, c;
    int z = 0;
    while (cin >> r >> c) {
        if (!r && !c) break;
        int num;
        cin >> num;
        for (int i = 0; i < num; i++) {
            string s;
            cin >> s;
            rec[i].op = s;
            if (s == "EX") {
                for (int j = 0; j < 4; j++) {
                    cin >> rec[i].v[j];
                }
            } else {
                cin >> rec[i].num;
                for (int j = 0; j < rec[i].num; j++) {
                    cin >> rec[i].v[j];
                }
            }
        }
        int q;
        cin >> q;
        int x, y;
        if (z++) printf("\n");
        printf("Spreadsheet #%d\n", z);
        while (q--) {
            cin >> x >> y;
            printf("Cell data in (%d,%d) ", x, y);
            int ok = 0;
            for (int i = 0; i < num; i++) {
                if (rec[i].op == "EX") {
                    int r1 = rec[i].v[0], c1 = rec[i].v[1];
                    int r2 = rec[i].v[2], c2 = rec[i].v[3];
                    if (r1 == x && c1 == y) {
                        x = r2;
                        y = c2;
                    } else if (r2 == x && c2 == y) {
                        x = r1;
                        y = c1;
                    }
                } else {
                    int m1 = 0, m2 = 0;
                    int e1 = 0, e2 = 0;
                    for (int j = 0; j < rec[i].num; j++) {
                        int t = rec[i].v[j];
                        if (t <= x) m1++;
                        if (t <= y) m2++;
                        if (t == x) e1 = 1;
                        if (t == y) e2 = 1;
                    }
                    if (rec[i].op == "DR") {
                        if (e1) {
                            ok = 1;
                            break;
                        } else x -= m1;
                    } else if (rec[i].op == "DC") {
                        if (e2) {
                            ok = 1;
                            break;
                        } else y -= m2;
                    } else if (rec[i].op == "IR") x += m1;
                    else if (rec[i].op == "IC") y += m2;
                }
            }
            if (ok) printf("GONE\n");
            else printf("moved to (%d,%d)\n", x, y);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值