AC代码:
/*
* 20171030
*/
//#define LOCAL
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;
#define maxn 10000
struct Command
{
char s[5];
int r1,c1,r2,c2;
int A;
int a[15];
}cmd[maxn];
int loc = 0;
void solve(int x,int y)
{
int tx = x;
int ty = y;
int tmp = 0;
for(int i=0;i<loc;i++)
{
//exchange
if(cmd[i].s[0]=='E')
{
//printf("!!! EX %d %d %d %d tx:%d ty:%d\n",cmd[i].r1,cmd[i].c1,cmd[i].r2,cmd[i].c2,tx,ty);
//the first condition
if(tx==cmd[i].r1&&ty==cmd[i].c1){tx=cmd[i].r2;ty=cmd[i].c2;}
//the second condition
else if(tx==cmd[i].r2&&ty==cmd[i].c2){tx=cmd[i].r1;ty=cmd[i].c1;}
}
//delete
if(cmd[i].s[0]=='D')
{
//delete r
if(cmd[i].s[1]=='R')
{
tmp = 0;
for(int j=0;j<cmd[i].A;j++)
{
if(tx>cmd[i].a[j]) tmp++;
if(tx==cmd[i].a[j]){printf("Cell data in (%d,%d) GONE\n",x,y);return ;}
}
tx-=tmp;
}
//delete c
if(cmd[i].s[1]=='C')
{
tmp = 0;
for(int j=0;j<cmd[i].A;j++)
{
if(ty>cmd[i].a[j]) tmp++;
if(ty==cmd[i].a[j]){ printf("Cell data in (%d,%d) GONE\n",x,y);return ;}
}
ty-=tmp;
}
}
//insert
if(cmd[i].s[0]=='I')
{
if(cmd[i].s[1]=='R')
{
tmp = 0;
for(int j=0;j<cmd[i].A;j++) if(tx>=cmd[i].a[j]) tmp++;
tx+=tmp;
}
if(cmd[i].s[1]=='C')
{
tmp = 0;
for(int j=0;j<cmd[i].A;j++) if(ty>=cmd[i].a[j]) tmp++;
ty+=tmp;
}
}
//printf("!!! %d %d\n",tx,ty);
}
printf("Cell data in (%d,%d) moved to (%d,%d)\n",x,y,tx,ty);
}
int main()
{
#ifdef LOCAL
freopen("test.txt","r",stdin);
freopen("b.txt","w",stdout);
#endif
int r,c;
int cnt = 0;
while(scanf("%d%d",&r,&c)==2&&r)
{
loc = 0;
memset(cmd,0,sizeof(cmd));
int n;
if(cnt>0) printf("\n");
printf("Spreadsheet #%d\n",++cnt);
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%s",cmd[loc].s);
if(cmd[loc].s[0]=='E')
{
scanf("%d%d%d%d",&cmd[loc].r1,&cmd[loc].c1,&cmd[loc].r2,&cmd[loc].c2);
}
else
{
scanf("%d",&cmd[loc].A);
for(int j=0;j<cmd[loc].A;j++)
{
scanf("%d",&cmd[loc].a[j]);
}
}
loc++;
}
scanf("%d",&n);
for(int i=0;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
solve(x,y);
}
}
return 0;
}
随机生成测试数据代码,用于与AC代码文件对碰,文件对碰代码http://blog.csdn.net/shelldawn/article/details/78401066
/*
* 20171030
*/
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <algorithm>
using namespace std;
char str[5][5] = {"EX","DR","DC","IC","IR"};
const int allnum = 10;
//0<=n<=l
void solve(int n,int l)
{
int last = 0;
int now = 0;
for(int i=1;i<=n;i++)
{
now = last+rand()%(l-(n-i)-last)+1;
printf(" %d",now);
last = now;
}
}
int main()
{
freopen("test.txt","w",stdout);
srand(time(NULL));
for(int i=0;i<allnum;i++)
{
int r = rand()%50+1;
int c = rand()%50+1;
int tr = r;
int tc = c;
printf("%d %d\n",r,c);
printf("%d\n",allnum);
for(int j=0;j<allnum;j++)
{
int k = rand()%5;
if(k==0)
{
if(tr==0||tc==0) j--;
else
{
printf("%s",str[k]);
printf(" %d %d %d %d\n",rand()%tr+1,rand()%tc+1,rand()%tr+1,rand()%tc+1);
}
}else if(k==1)
{
printf("%s",str[k]);
int f = min(15,rand()%(tr+1));
printf(" %d",f);
solve(f,tr);
tr-=f;
}else if(k==2)
{
printf("%s",str[k]);
int f = min(15,rand()%(tc+1));
printf(" %d",f);
solve(f,tc);
tc-=f;
}else if(k==3)
{
printf("%s",str[k]);
int f = min(15,rand()%(tc+1));
printf(" %d",f);
solve(f,tc);
tc+=f;
}else
{
printf("%s",str[k]);
int f = min(15,rand()%(tr+1));
printf(" %d",f);
solve(f,tr);
tr+=f;
}
printf("\n");
}
printf("%d\n",allnum);
for(int j=0;j<allnum;j++)
{
printf("%d %d\n",rand()%r+1,rand()%c+1);
}
}
printf("0 0\n");
return 0;
}