本题让人心烦的事:①有很多操作,所以需要再这些操作中找到共同的东西。真的非常有难度
②插入/删除多行该如何实现,若只是采用原始的换值,或者覆盖,则需要做很多的限定。其实题目有提示的,就是说要保留原来的数组内容,再在另外一个数组上进行操作。所以这里是采用将其他数组里对应的赋值给改变的数组。
③答案给了一个非常巧妙的方法,找到了它们之间的共同点。用了一个小变量cnt2!
④下面给出的方法还是有一点不,就是有点慢。一旦做了删除或者什么操作,就要重新改变数组里几乎全部的数组。
#include <stdio.h>
#include <string.h>
#define maxn 100
#define BIG 10000
int r, c, n, d[maxn][maxn], d2[maxn][maxn], ans[maxn][maxn], cols[maxn];
void copy(char type, int p, int q)
{
if(type == 'R')
{
for(int i = 1; i <= c; i++) //将q行赋值给p行,注意两个数组是不一样的
d[p][i] = d2[q][i];
}
else
{
for(int i = 1; i <= r; i++) //将q列赋值给p列
d[i][p] = d2[i][q];
}
}
void del(char type)
{
memcpy(d2,d,sizeof(d2));
int cnt = (type == 'R' ? r : c), cnt2 = 0;
for(int i = 1; i <= cnt; i++)
{
if(!cols[i])
copy(type, ++cnt2, i); //巧妙!cnt2的巧妙运用
}
if(type == 'R')
r = cnt2;
else
c = cnt2;
}
void ins(char type)
{
memcpy(d2,d,sizeof(d2));
int cnt = (type == 'R' ? r : c), cnt2 = 0;
for(int i = 1; i <= cnt; i++)
{
if(cols[i])
copy(type, ++cnt2, 0);
copy(type, ++cnt2, 0);
}
if(type == 'R')
r = cnt2;
else
c = cnt2;
}
int main()
{
int r1,c1,r2,c2,q,kase = 0;
memset(d,0,sizeof(d));
char cmd[10]; //用做接受命令
while(scanf("%d%d%d", &r, &c, &n) == 3 && r)
{
for(int i = 1; i <= r; i++)
{
for(int j = 1; j <= c; j++)
d[i][j] = i * BIG + j;
}
while(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);
for(int i = 0; i < a; i++)
{
scanf("%d", &x);
cols[x] = 1;
}
if(cmd[0] == 'D')
del(cmd[1]);
else
ins(cmd[1]);
}
}
//查询操作
memset(ans,0,sizeof(ans));
for(int i = 1; i <= r; i++)
{
for(int j = 1; j <= c; j++)
ans[d[i][j] / BIG][d[i][j] % BIG] = i * BIG + j;
}
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;
}