UVa 512 - Spreadsheet Tracking

Contents

  1. 1. Problem
  2. 2. Solution
  3. 3. Code

Problem

題目網址

刪除和插入多個時,都是同時完成,要注意其位置。

Solution

把每個指令記錄下來,再詢問時每次都跑一次,看詢問的點最後在哪裡,不需要記整個表格。
例如在第 5 列的點,刪除 1~4 列時會使其變成在第4列。

Code

UVa 512
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
#include<cstdio>

struct Command
{
char com[3];
int count, x[10];
}command[500];
inline void simulate(int com, int& r, int& c)
{
int i, j;
for (i = 0; i < com; i++)
{
int temp = 0;
if (command[i].com[0] == 'D'&&command[i].com[1] == 'R')
{
for (j = 0; j < command[i].count; j++)
if (r == command[i].x[j])
{
r = c = 0;
return;
}
else if (r > command[i].x[j])
temp++;
r -= temp;
}
else if (command[i].com[0] == 'D'&&command[i].com[1] == 'C')
{
for (j = 0; j < command[i].count; j++)
if (c == command[i].x[j])
{
r = c = 0;
return;
}
else if (c > command[i].x[j])
temp++;
c -= temp;
}
else if (command[i].com[0] == 'I'&&command[i].com[1] == 'R')
{
for (j = 0; j < command[i].count; j++)
if (r >= command[i].x[j])
temp++;
r += temp;
}
else if (command[i].com[0] == 'I'&&command[i].com[1] == 'C')
{
for (j = 0; j < command[i].count; j++)
if (c >= command[i].x[j])
temp++;
c += temp;
}
else if (r == command[i].x[0] && c == command[i].x[1])
r = command[i].x[2], c = command[i].x[3];
else if (r == command[i].x[2] && c == command[i].x[3])
r = command[i].x[0], c = command[i].x[1];
}

}
int main()
{
int row, col, Case = 1;
while (scanf("%d%d", &row, &col) && row)
{
int com, i;
scanf("%d", &com);//command
for (i = 0; i < com; i++)
{
scanf("%s%d", command[i].com, &command[i].count);

if (command[i].com[0] == 'E')
{
command[i].x[0] = command[i].count;
scanf("%d%d%d", &command[i].x[1], &command[i].x[2], &command[i].x[3]);
}
else
for (int j = 0; j < command[i].count; j++)
scanf("%d", &command[i].x[j]);
}

if (Case != 1)
putchar('\n');
printf("Spreadsheet #%d\n", Case++);
int r, c, query;
scanf("%d", &query);//query
for (i = 0; i < query; i++)
{
scanf("%d%d", &r, &c);
int rr = r, cc = c;
simulate(com, r, c);
if (r)
printf("Cell data in (%d,%d) moved to (%d,%d)\n", rr, cc, r, c);
else
printf("Cell data in (%d,%d) GONE\n", rr, cc);
}
}

return 0;
}