512 - Spreadsheet Tracking

Spreadsheet Tracking

PS:因为该题排版较麻烦,这里给出OJ网址:UVa512 - Spreadsheet Tracking

#include <stdio.h>
#include <string.h>
#define maxNum 10005
// 行,列,操作数
int r, c, n;
struct Command {
    // 命令名
    char c[5];
    // exchange操作参数
    int r1, c1, r2, c2;
    // d,i操作参数
    int a, x[20];
} cmd[maxNum];

int simulate(int *r0, int *c0) {
    for(int i = 0; i < n; i++) {
        // exchange
        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 {
            // delete or insert
            // (r0,c0)变动的行列数
            int dr = 0, dc = 0;
            for(int j = 0; j < cmd[i].a; j++) {
                // 操作第几行或列
                int x = cmd[i].x[j];
                // insert
                if(cmd[i].c[0] == 'I') {
                    // insert row and less than r0
                    if(cmd[i].c[1] == 'R' && x <= *r0) {
                        dr++;
                    }
                    // insert column and less than c0
                    if(cmd[i].c[1] == 'C' && x <= *c0) {
                        dc++;
                    }
                } else {
                    // delete
                    // delete row and equal r0
                    if(cmd[i].c[1] == 'R' && x == *r0) {
                        return 0;
                    }
                    // delete row and less than r0
                    if(cmd[i].c[1] == 'R' && x < *r0) {
                        dr--;
                    }
                    // delete column and equal c0
                    if(cmd[i].c[1] == 'C' && x == *c0) {
                        return 0;
                    }
                    // delete column and less than c0
                    if(cmd[i].c[1] == 'C' && x < *c0) {
                        dc--;
                    }
                }
            }
            *r0 += dr;
            *c0 += dc;
        }
    }
    return 1;
}



int main() {
    // 查询cell行,查询cell列,查询cell次数
    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\n");
            } else {
                printf("moved to (%d,%d)\n", r0, c0);
            }
        }
    }
    return 0;
}

你可能感兴趣的:(ACM,uva,UVa512)