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.
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 (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 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
/************************************************** filename :g.cpp author :maksyuki created time :2017/12/5 11:03:02 last modified :2017/12/5 22:39:32 file location :C:\Users\abcd\Desktop\TheEternalPoet ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CFO freopen ("out", "w", stdout) #define CPPFF ifstream cin ("in") #define CPPFO ofstream cout ("out") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define DBT printf("time used: %.2lfs\n", (double) clock() / CLOCKS_PER_SEC) #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 6666; char cmd[maxn][6]; int v[maxn][12], row, col, na, nb; int ans[6]; int calca(int vv, int p) { int cnt = 0; for(int i = 1; i <= v[vv][0]; i++) { if(ans[p] == v[vv][i]) return -1; if(ans[p] > v[vv][i]) cnt++; } return cnt; } int calcb(int vv, int p) { int cnt = 0; for(int i = 1; i <= v[vv][0]; i++) if(ans[p] >= v[vv][i]) cnt++; return cnt; } bool findv(int pa, int pb) { ans[1] = pa, ans[2] = pb; for(int i = 1; i <= na; i++) { //printf("(%d,%d)\n", ansa, ansb); if(cmd[i][0] == 'E') { if(ans[1] == v[i][1] && ans[2] == v[i][2]) ans[1] = v[i][3], ans[2] = v[i][4]; else if(ans[1] == v[i][3] && ans[2] == v[i][4]) ans[1] = v[i][1], ans[2] = v[i][2]; } else { if(cmd[i][0] == 'D') { if(cmd[i][1] == 'R') { int tmp = calca(i, 1); if(tmp == -1) return false; ans[1] -= tmp; } else { int tmp = calca(i, 2); if(tmp == -1) return false; ans[2] -= tmp; } } if(cmd[i][0] == 'I') { if(cmd[i][1] == 'R') ans[1] += calcb(i, 1); else ans[2] += calcb(i, 2); } } } return true; } int main() { #ifdef LOCAL CFF; //CFO; #endif int id = 1; while(scanf(" %d %d", &row, &col) != EOF) { if(!row && !col) break; scanf(" %d", &na); for(int i = 1; i <= na; i++) { scanf(" %s", cmd[i]); if(cmd[i][0] == 'E') for(int j = 1; j <= 4; j++) scanf(" %d", &v[i][j]); else { int tmp; scanf(" %d", &tmp); v[i][0] = tmp; for(int j = 1; j <= tmp; j++) scanf(" %d", &v[i][j]); } } if(id > 1) puts(""); printf("Spreadsheet #%d\n", id++); scanf(" %d", &nb); int rr, cc; for(int i = 1; i <= nb; i++) { scanf(" %d %d", &rr, &cc); printf("Cell data in (%d,%d)", rr, cc); if(findv(rr, cc)) printf(" moved to (%d,%d)\n", ans[1], ans[2]); else puts(" GONE"); } } return 0; } |