https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=453
法一:算出變化後的表格
#include<stdio.h> #include<string.h> #define maxd 100 #define BIG 10000 //注意這裏取10000 d中的萬位爲行座標,個位爲縱座標 int r, c, n, d[maxd][maxd], d2[maxd][maxd], ans[maxd][maxd], cols[maxd]; //此處cols儲存操做行或者列的位置,須要操做的爲1,其他爲0 void copy(char type, int p, int q) { if(type == 'R') { for(int i = 1; i <= c; i++) d[p][i] = d2[q][i]; //複製行到行 } else { for(int i = 1; i <= r; i++) //複製列到列 d[i][p] = d2[i][q]; } } void del(char type) { memcpy(d2, d, sizeof(d)); int cnt = type == 'R' ? r : c, cnt2 = 0; for(int i = 1; i <= cnt; i++) { if(!cols[i]) copy(type, ++cnt2, i); //遇到1時,cnt2少加一次,i正常遞增 } if(type == 'R') r = cnt2; else c = cnt2; //新的行數或者列數 } void ins(char type) { memcpy(d2, d, sizeof(d)); int cnt = type == 'R' ? r : c, cnt2 = 0; for(int i = 1; i <= cnt; i++) { if(cols[i]) copy(type, ++cnt2, 0); //遇到1時,cnt2 多加一次, copy(type, ++cnt2, i); //i正常遞增 因爲將0行或是0列移動到增長的位置,因此增長的空位置內填入的都是0 } if(type == 'R') r = cnt2; else c = cnt2; //新的行數或者列數 } int main() { int r1, c1, r2, c2, q, kase = 0; char cmd[10]; memset(d, 0, sizeof(d)); while(scanf("%d%d%d", &r, &c, &n) == 3 && r) { int r0 = r, c0 = c; for(int i = 1; i <= r; i++) for(int j = 1; j <= c; j++) d[i][j] = i*BIG + j; //爲d賦值,第一行10001,10002……第二行20001,20002…… while(n--) { //接下來輸入n個操做語句 scanf("%s", cmd); if(cmd[0] == 'E') { scanf("%d%d%d%d", &r1, &c1, &r2, &c2); int t = d[r1][c1]; d[r1][c1] = d[r2][c2]; d[r2][c2] = t; } else { int a, x; scanf("%d", &a); memset(cols, 0, sizeof(cols)); for(int i = 0; i < a; i++) { scanf("%d", &x); cols[x] = 1; } if(cmd[0] == 'D') del(cmd[1]); else ins(cmd[1]); //cmd存儲操做行爲的字符DR、DC、IR、IC。經過cmd[0]和cmd[1]共同組成判斷條件 } } memset(ans, 0, sizeof(ans)); for(int i = 1; i <= r; i++) for(int j = 1; j <= c; j++) {//原來是30001,上移1行,賦值20001,即從(3,1)變爲(2,1)
//d[i][j]值 /10000 %10000 存儲的是原來的行數和列數 ans[d[i][j]/BIG][d[i][j]%BIG] = i*BIG+j; //ans存儲通過增減調換後的數組,ans的內容爲變化後的數組d的座標 } if(kase > 0) printf("\n"); printf("Spreadsheet #%d\n", ++kase); scanf("%d", &q); while(q--) { scanf("%d%d", &r1, &c1); printf("Cell data in (%d,%d) ", r1, c1); if(ans[r1][c1] == 0) printf("GONE\n"); else printf("moved to (%d,%d)\n", ans[r1][c1]/BIG, ans[r1][c1]%BIG); //查找該數在變化後的數組中的位置,輸出座標 } } return 0; }
法二:追蹤電子表格中單元格的變化php
#include<stdio.h> #include<string.h> #define maxd 1000 struct Command{ char c[5]; int r1,c1,r2,c2; int a,x[20]; }cmd[maxd]; 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==*r0) { *r0==cmd[i].r1;*c0=cmd[i].c1; } } else{ int dr,dc=0; for(int j=0;j<cmd[i].a;j++) { int x=cmd[i].x[j]; if(cmd[i].c[0]=='I')//IR 2 3 { 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() { 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);//R 1 2 3 4 if(cmd[i].c[0]=='E'){ scanf("%d%d%d%d",&cmd[i].r1,&cmd[i].c1,&cmd[i].r2,&cmd[i].c2);} else{ //IR 2 1 2 DR 3 1 2 4 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); } } return 0; }