UVA 512 tracking cells in a spreadsheet

Source: Internet
Author: User

Spreadsheet Tracking

Data in spreadsheets is stored in cells, which is organized in rows (r) and columns (C). Some operations on spreadsheets can is applied to single cells (R,C), while others can is applied to ENT IRE 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 the 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 in the right.



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


1 2 3 4 5
1 2 24 8 22 16
2 18 19 21st 22 25
3 24 25 67 22 71
4 16 12 10 22 58
5 33 34 36 22 40

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 cells and cell (6,5), the spreadsheet looks like the one in the right.



You must write tracking software this determines the final location of data in spreadsheets this result from row, column, and exchange operations similar to the ones illustrated here.

InputThe 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 (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 are limited to 50. The following n lines specify the desired operations.


An operation to exchange the contents of cells (R1, C1) with the contents of cell (R2, c2) is given by:


EX r1C1r2C2


The four insert and delete commands--DC (delete columns), DR (delete rows), IC (Insert columns) , and IR (insert rows) is given by:


<command> Ax1x2xA


where <command> is one of the four commands; a is a positive integer less than, and was 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 would be is unique.


The operations is followed by a integer which is the number of queries for the spreadsheet. Each query consists of positive integers Rand 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 The data is originally in cell (R, C). The end of input is indicated by a row consisting of a pair of zeros for the spreadsheet dimensions.

Outputfor each spreadsheet, the your program must output it 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 GONEif 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 would not be contain a sequence of commands that would cause the spreadsheet to exceed the maximum size.

Sample Input
7 95DR   2  1 5DC  4  3 6 7 9IC  1  3IR  2  2 4EX 1 2 6 544 85 57 86 50 0

Sample Output
Spreadsheet #1Cell data in (4,8) moved to (4,6) cell data in (5,5) Gonecell data in (7,8) moved to (7,6) cell data in (6,5) Moved to (ON)

#include <iostream> #include <stdio.h> #include <string> #include <cstring> #include <    algorithm> #include <cmath> #define N 10009using namespace std;struct command{Char c[50];    int r1,c1,r2,c2; int a,x[50];}            Cmd[n];int R,c,n;int Simulate (int* r0,int* C0) {for (int i=0;i<n;i++) {if (cmd[i].c[0]== ' E ') {                if (cmd[i].r1==*r0 && cmd[i].c1==*c0) {*r0=cmd[i].r2;            *C0=CMD[I].C2;                } else if (cmd[i].r2==*r0 && cmd[i].c2==*c0) {*r0=cmd[i].r1;            *C0=CMD[I].C1;                }} else {int dr=0,dc=0;                    for (int j=0;j<cmd[i].a;j++) {int x=cmd[i].x[j];                        if (cmd[i].c[0]== ' I ') {if (cmd[i].c[1]== ' R ' && x<=*r0) dr++; if (cmd[i].c[1]== ' C ' && x<=*c0) dc++;                        } else {if (cmd[i].c[1]== ' R ' && x==*r0) return 0;                        if (cmd[i].c[1]== ' C ' && x==*c0) return 0;                        if (cmd[i].c[1]== ' R ' && x<*r0) dr--;                    if (cmd[i].c[1]== ' C ' && x<*c0) dc--;  }} *R0+=DR;            *C0+=DC; }} return 1;}    int main () {int r0,c0,q,ca=0;        while (scanf ("%d%d", &r,&c)!=eof) {if (r+c==0) break;        scanf ("%d", &n);            for (int i=0;i<n;i++) {scanf ("%s", cmd[i].c);            if (cmd[i].c[0]== ' E ') scanf ("%d%d%d%d", &AMP;CMD[I].R1,&AMP;CMD[I].C1,&AMP;CMD[I].R2,&AMP;CMD[I].C2);                else {scanf ("%d", &cmd[i].a);            for (int j=0;j<cmd[i].a;j++) scanf ("%d", &cmd[i].x[j]);   }        }     if (ca>0) cout<<endl;        printf ("Spreadsheet #%d\n", ++CA);        scanf ("%d", &q);            while (q--) {scanf ("%d%d", &r0,&c0);            printf ("Cell data in (%d,%d)", r0,c0);            if (!simulate (&r0,&c0)) cout<< "GONE" <<endl;        else printf ("moved to (%d,%d) \ n", r0,c0); }} return 0;}






UVA 512 tracking cells in a spreadsheet

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.