• Uva


    Data in spreadsheets are stored in cells, which are organized in rows (r) and columns (c). 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.


    $	extstyle parbox{.5	extwidth}{egin{center}egin{tabular}{rvert rvert ......4 & 35 & 36 & 22 & 38 & 39 & 40 & 41\ cline{2-10}end{tabular}end{center}}$ $	extstyle parbox{.49	extwidth}{egin{center}egin{tabular}{rvert rvert......4 & 35 & 36 & 22 & 38 & 39 & 40 & 41\ cline{2-10}end{tabular}end{center}}$


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


    $searrow$ 1 2 3 4 5
    1 2 24 8 22 16
    2 18 19 21 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 cell (1,2) and cell (6,5), the spreadsheet looks like the one on the right.


    $	extstyle parbox{.33	extwidth}{egin{center}egin{tabular}{rvert rvert......cline{2-6}8 & 33 & 34 & 36 & 22 & 40\ cline{2-6}end{tabular}end{center}}$ $	extstyle parbox{.33	extwidth}{egin{center}egin{tabular}{rvert rvert......ine{2-7}8 & 33 & 34 & & 36 & 22 & 40\ cline{2-7}end{tabular}end{center}}$ $	extstyle parbox{.32	extwidth}{egin{center}egin{tabular}{rvert rvert......ine{2-7}8 & 33 & 34 & & 36 & 22 & 40\ cline{2-7}end{tabular}end{center}}$


    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 (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 the desired operations.


    An operation to exchange the contents of cell (r1c1) 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:


    <commandA x1 x2 $dots$ xA


    where <command> is one of the four commands; A is a positive integer less than 10, and$x_1, dots, x_A$ 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 (rc). 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)
    先把命令保存下来,每次询问之后对询问位置执行一次操作,效率也不错。

    AC代码:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cctype>
    #include <cstring>
    #include <string>
    #include <sstream>
    #include <vector>
    #include <set>
    #include <map>
    #include <algorithm>
    
    using namespace std;
    
    const int maxd = 10000;
    
    struct Command
    {
    	char c[5];
    	int r1, c1, r2, c2;
    	int a, x[20];
    } cmd[maxd];
    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, kase = 0;
    	while (scanf("%d%d", &r, &c) == 2 && r) {
    		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", &cmd[i].r1, &cmd[i].c1, &cmd[i].r2, &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 (kase > 0) printf("
    ");
    		printf("Spreadsheet #%d
    ", ++kase);
    		scanf("%d", &q);
    		while (q--) {
    			scanf("%d%d", &r0, &c0);
    			printf("Cell data in (%d,%d) ", r0, c0);
    			// 对每个查找进行一次完整的操作
    			if (!simulate(r0, c0)) {
    				printf("GONE
    ");
    			}
    			else {
    				printf("moved to (%d,%d)
    ", r0, c0);
    			}
    		}
    	}
    
    	return 0;
    }




  • 相关阅读:
    os/exec 执行命令的五种姿势
    巧用SSH打通外网限制(重要)
    go的多个返回值。。。
    go 的return值在defer前返回,defer延迟执行
    [一、基础控件]5使视图充满整个屏幕
    [一、基础控件]3Text视图的段落属性
    [一、基础控件]8使用Label显示图像、SF符号和文字的组合
    [一、基础控件]10使用SecureField密文输入框接收用户的密码
    [一、基础控件]11创建可容纳多行滚动文字的输入框TextEditor
    [一、基础控件]7使用Text视图创建漂亮的富文本
  • 原文地址:https://www.cnblogs.com/zhangyaoqi/p/4591596.html
Copyright © 2020-2023  润新知