模拟,先处理表格后面的行和列再处理前面的(无论是插入还是删除),否则操作会出问题。
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; #define _for(i, a, b) for (int i = (a); i <= (b);i++) struct sheet{ int x, y; } c[55][55];//记录下原始下标 int n, m,row,col; void init(){ for (int i = 1; i <= 54;i++){ for (int j = 1; j <= 54;j++){ c[i][j].x = i; c[i][j].y = j; } } }//赋初值 void ir(){ int a[10]; int num; cin >> num; _for(i,0,num-1){ cin >> a[i]; } sort(a, a + num); for (int i = num - 1; i >= 0;i--){ for (int j = row; j >= a[i];j--){ for (int k = 1; k <= col;k++){ c[j + 1][k].x = c[j][k].x; c[j + 1][k].y = c[j][k].y; } }//往下移一格,后面的先移 for (int k = 1; k <= col;k++){ c[a[i]][k].x = c[a[i]][k].y=0; }//添加的空白行置为0 row++;//行数加一 }}; void ic(){ int a[10]; int num; cin >> num; _for(i, 0, num-1) { cin >> a[i]; } sort(a, a + num); for (int i = num - 1; i >= 0;i--){ for (int j = col; j >= a[i];j--){ for (int k = 1; k <= row;k++){ c[k][j + 1].x = c[k][j].x; c[k][j + 1].y = c[k][j].y; } } for (int k = 1; k <= row;k++){ c[k][a[i]].x = 0; c[k][a[i]].y = 0; } col++; } }; void dr(){ int a[10]; int num; cin >> num; _for(i, 0, num-1) { cin >> a[i]; } sort(a, a + num); for (int i = num - 1; i >= 0;i--){ for (int j = a[i] + 1; j <= row;j++){ for (int k = 1; k <= col;k++){ c[j-1][k].x = c[j][k].x; c[j-1][k].y = c[j][k].y; } } row--; } }; void dc(){ int a[10]; int num; cin >> num; _for(i, 0, num-1) { cin >> a[i]; } sort(a, a + num); for (int i = num - 1; i >= 0;i--){ for (int j = a[i] + 1; j <= col;j++){ for (int k = 1; k <= row;k++){ c[k][j - 1].x = c[k][j].x; c[k][j - 1].y = c[k][j].y; } } col--; } }; int main(){ int kase = 0; while(scanf("%d%d",&n,&m)==2&&(n||m)){ row = n, col = m; init(); int t; scanf("%d", &t); while(t--){ string op; cin >> op; if(op=="IR"){ ir(); }else if(op=="IC"){ ic(); }else if(op=="DR"){ dr(); }else if(op=="DC"){ dc(); }else{ int x1, x2, y1, y2,t1,t2; cin >> x1 >> y1 >> x2 >> y2; t1 = c[x1][y1].x, t2 = c[x1][y1].y; c[x1][y1].x = c[x2][y2].x, c[x1][y1].y = c[x2][y2].y; c[x2][y2].x = t1, c[x2][y2].y=t2; } } if(kase>0) puts(""); printf("Spreadsheet #%d\n", ++kase); int q; cin >> q;
while (q--) { int x1, y1,flag=0; cin >> x1 >> y1; for(int i=1;i<=row&&!flag;i++){ for(int j=1;j<=col&&!flag;j++){ if(c[i][j].x==x1&&c[i][j].y==y1){ flag = 1; printf("Cell data in (%d,%d) moved to (%d,%d)\n", c[i][j].x, c[i][j].y,i,j); } } } if(!flag) printf("Cell data in (%d,%d) GONE\n",x1,y1); } } return 0; }