您的位置:首页 > 其它

UVa_512 - Spreadsheet Tracking

2014-10-21 23:40 483 查看



Spreadsheet Tracking

Data in spreadsheets are stored in cells, which are organized in rows (r) andcolumns (c). Some operations onspreadsheets can be applied to single cells (r,c), while others can beapplied to entire rows or columns. Typical
celloperations include inserting and deleting rows or columns and exchanging cellcontents.

Some spreadsheets allow users to mark collections of rows or columns fordeletion, so the entire collection can bedeleted at once. Some (unusual) spreadsheets allow users to mark collectionsof rows or columns for insertions too.Issuing an insertion command results
in new rows or columns being insertedbefore each of the marked rows orcolumns. Suppose, for example, the user marks rows 1 and 5 of the spreadsheeton the left for deletion. Thespreadsheet then shrinks to the one on the right.



If the user subsequently marks columns 3, 6, 7, and 9 for deletion, thespreadsheet shrinks to this.


12345
122482216
21819212225
32425672271
41612102258
53334362240
If the user marks rows 2, 3 and 5 for insertion, the spreadsheet grows tothe one on the left. If the user then markscolumn 3 for insertion, the spreadsheet grows to the one in the middle.Finally, if the user exchanges the contents ofcell (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 datain 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 thosespreadsheets, and queries about them. Eachspreadsheet definition begins with a pair of integers specifying its initialnumber of rows (r) and columns (c),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 thedesired operations.

An operation to exchange the contents of cell (r1,c1) with the contentsof cell (r2,c2)
is given by:

EX r1 c1r2
c2

The four insert and delete commands--DC (delete columns), DR (delete rows),IC (insert columns), andIR (insert rows) are given by:

<command> A x1
x2

xA

where <command> is one of the four commands; A is a positive integer lessthan 10, and

are the labels ofthe
columns or rows to be deleted or inserted before. For each insert anddelete command, the order of the rows orcolumns in the command has no significance. Within a single delete or insertcommand, labels will be unique.

The operations are followed by an integer which is the number of queries forthe spreadsheet. Each query consists ofpositive integersr and
c, representing the row and column number of a cellin the original spreadsheet. For eachquery, your program must determine the current location of the data thatwas originally in cell (r,c). The end ofinput is indicated by a row consisting
of a pair of zeros for the spreadsheetdimensions.

Output

For each spreadsheet, your program must output its sequence number (startingat 1). For each query, your programmust output the original cell location followed by the final location ofthe data or the wordGONE if the contents ofthe 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 thespreadsheet 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)


题意:

有一个r行c列的电子表格,对其进行n个操作。操作类型分为5种:

1. 删除行 2. 删除列 3. 插入行 4. 插入列 5. 交换两个元素位置

输入初始查找位置,输出经过n个操作后,变更的位置

解题:

1. 可以直接模拟,但是需要开很多额外空间;

2. 先将n个操作存起来,然后对每个查询分别执行这个n个操作,输出变更后位置。这样节约空间,也很简单方便

注意:

1. 在执行DR,DC,IR,IC操作时,必须先将位置(x,y)保存下,见代码,即:xx = x, yy = y;(因为这四个操作是基于初始的位置x,y进行的)

2. 不同的测试用例用空行隔开,最后一个用例不能有空行,开始Wrong answer就因为最后多输出一空行

代码如下:

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;

const int MAX = 10000;
int r,c,n;
struct cmd
{
string s;
int A,a[20];
int r1,c1,r2,c2;
}cmds[MAX];

void save(string str, int i)
{
int A;
if(str!="EX"){
cin>>A;
cmds[i].s = str; cmds[i].A = A;
for(int j=0;j<A;j++) cin>>cmds[i].a[j];
}
else if(str=="EX"){
cmds[i].s = str;
cin>>cmds[i].r1>>cmds[i].c1>>cmds[i].r2>>cmds[i].c2;
}
}

bool solve(int& x, int& y)
{
int xx,yy;
for(int i=0;i<n;i++)
{
if(cmds[i].s=="DR"){
xx = x; yy = y;
for(int j=0;j<cmds[i].A;j++){
if(cmds[i].a[j]<xx) { x--; }
else if(cmds[i].a[j]==xx) { return false;}
}
}
else if(cmds[i].s=="DC"){
xx = x; yy = y;
for(int j=0;j<cmds[i].A;j++){
if(cmds[i].a[j]<yy)  {  y--;}
else if(cmds[i].a[j]==yy) { return false;}
}
}
else if(cmds[i].s=="IC"){
xx = x; yy = y;
for(int j=0;j<cmds[i].A;j++){
if(cmds[i].a[j]<=yy) y++;
}
}
else if(cmds[i].s=="IR"){
xx = x; yy = y;
for(int j=0;j<cmds[i].A;j++){
if(cmds[i].a[j]<=xx)  x++;
}
}
else if(cmds[i].s=="EX"){
if(x==cmds[i].r1&&y==cmds[i].c1) { x = cmds[i].r2; y =cmds[i].c2;}
else if(x==cmds[i].r2&&y==cmds[i].c2) { x = cmds[i].r1; y =cmds[i].c1;}
}
}
return true;
}

int main()
{
//freopen("512.txt","r",stdin);
//freopen("512ans.txt","w",stdout);
int icase = 0;
while(cin>>r>>c)
{
if(r==0&&c==0) break;
cin>>n;
if(icase>0) cout<<endl;
for(int i=0;i<n;i++)
{
string str; cin>>str;
save(str,i);
}
cout<<"Spreadsheet #"<<++icase<<endl;
int q; cin>>q;
while(q--)
{
int x,y,xx,yy;
cin>>x>>y; xx = x; yy = y;
if(solve(x,y)) printf("Cell data in (%d,%d) moved to (%d,%d)\n",xx,yy,x,y);
else printf("Cell data in (%d,%d) GONE\n",xx,yy);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: