Time limit: 3.000 seconds
Problem Description
Data in spreadsheets are stored in cells, which are organized in rows ® and columns ©. Some operations on spreadsheets can be applied to single cells (r, c), while others can be applied to entire rows or columns. Typical cell operations include inserting and deleting rows or columns and exchanging cell contents.
Some spreadsheets allow users to mark collections of rows or columns for deletion, so the entire collection can be deleted at once. Some (unusual) spreadsheets allow users to mark collections of rows or columns for insertions too. Issuing an insertion command results in new rows or columns being inserted before each of the marked rows or columns. Suppose, for example, the user marks rows 1 and 5 of the spreadsheet on the left for deletion. The spreadsheet then shrinks to the one on the right.
If the user subsequently marks columns 3, 6, 7, and 9 for deletion, the spreadsheet shrinks to this.
If the user marks rows 2, 3 and 5 for insertion, the spreadsheet grows to the one on the left. If the user then marks column 3 for insertion, the spreadsheet grows to the one in the middle. Finally, if the user exchanges the contents of cell (1,2) and cell (6,5), the spreadsheet looks like the one on the right.
You must write tracking software that determines the final location of data in spreadsheets that result from row, column, and exchange operations similar to the ones illustrated here.
Input
The input consists of a sequence of spreadsheets, operations on those spreadsheets, and queries about them. Each spreadsheet definition begins with a pair of integers specifying its initial number of rows ® and columns ©, followed by an integer specifying the number (n) of spreadsheet operations. Row and column labeling begins with 1. The maximum number of rows or columns of each spreadsheet is limited to 50. The following n lines specify the desired operations.
An operation to exchange the contents of cell (r1, c1) with the contents of cell (r2, c2) is given by: EX r1 c1 r2 c2
The four insert and delete commands—DC (delete columns), DR (delete rows), IC (insert columns),and IR (insert rows) are given by:
< command > A x1 x2 . . . xA
where < command > is one of the four commands; A is a positive integer less than 10, and x1, . . . , xA are the labels of the columns or rows to be deleted or inserted before. For each insert and delete command, the order of the rows or columns in the command has no significance. Within a single delete or insert command, labels will be unique.
The operations are followed by an integer which is the number of queries for the spreadsheet. Each query consists of positive integers r and c, representing the row and column number of a cell in the original spreadsheet. For each query, your program must determine the current location of the data that was originally in cell (r, c). The end of input is indicated by a row consisting of a pair of zeros for the spreadsheet dimensions.
Output
For each spreadsheet, your program must output its sequence number (starting at 1). For each query, your program must output the original cell location followed by the final location of the data or the word ‘GONE’ if the contents of the original cell location were destroyed as a result of the operations. Separate output from different spreadsheets with a blank line.
The data file will not contain a sequence of commands that will cause the spreadsheet to exceed the maximum size.
Sample input
7 9
5
DR 2 1 5
DC 4 3 6 7 9
IC 1 3
IR 2 2 4
EX 1 2 6 5
4
4 8
5 5
7 8
6 5
0 0
Sample output
Spreadsheet #1
Cell data in (4,8) moved to (4,6)
Cell data in (5,5) GONE
Cell data in (7,8) moved to (7,6)
Cell data in (6,5) moved to (1,2)
1.通过复制实现表格行列的删除和增加
2.ans表下标表示原始位置,值表示最终位置
#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];//通过d,d2完成模拟操作
//ans表用来查询cols用来记录操作的行列
void copy(char type,int p,int q){
if(type=='R'){//复制d2表q行到d表p行
for( int i=1; i<=c; i++ ){d[p][i]=d2[q][i];}
}
else{//复制d2表q列到d表p列
for( int i=1; i<=r; i++ ){d[i][p]=d2[i][q];}
}
}
void del(char type){//通过copy函数实现删除
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);//不操作的行列复制,操作的行列不复制实现删除
}
if(type=='R') r=cnt2;else c=cnt2;
}
void ins(char type){//通过copy函数实现增加
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);//操作的行列复制0行或列实现增加
copy(type,++cnt2,i);
}
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){
for( int i=1; i<=r; i++ ){
for( int j=1; j<=c; j++ ){
d[i][j]=i*BIG+j;//对d表赋值,值表示原始单元格位置
}
}
while(n--){//对表操作n次
memset(cols,0,sizeof(cols));
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));//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);
if(kase>0) printf("\n");
printf("Spreadsheet #%d\n",++kase);
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;
}