# Examples 4-5 tracking cells in spreadsheets (ACM/ICPC World Finals 1997, UVa512)

Posted by raul_7 on Sat, 22 Feb 2020 17:10:30 +0100

Original question link: https://vjudge.net/problem/UVA-512
Classification: Functions
Note: complex simulation
Theoretically, it should be a water problem But I can't find the reason to simulate RE directly. Shamelessly, I recited the author's second method

The code is as follows:

```#include<stdio.h>
#include<string.h>
const int maxd = 10000;
int r, c, n, q, r0, c0, kase;
struct Command
{
char c[10];
int r1, c1, r2, c2;
int num, x[20];
}cmd[maxd];
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].num; j++)
{
if (cmd[i].c[0] == 'I')
{
if (cmd[i].c[1] == 'R' && cmd[i].x[j] <= *r0)dr++;
if (cmd[i].c[1] == 'C' && cmd[i].x[j] <= *c0)dc++;
}
else
{
if (cmd[i].c[1] == 'R' && cmd[i].x[j] == *r0)return 0;
if (cmd[i].c[1] == 'C' && cmd[i].x[j] == *c0)return 0;
if (cmd[i].c[1] == 'R' && cmd[i].x[j] < *r0)dr--;
if (cmd[i].c[1] == 'C' && cmd[i].x[j] < *c0)dc--;
}
}
*r0 += dr; *c0 += dc;
}
}
return 1;
}
int main(void)
{
while (scanf("%d%d%d", &r, &c, &n) == 3 && r)
{
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].num);
for (int j = 0; j < cmd[i].num; j++)
scanf("%d", &cmd[i].x[j]);
}
}
if (kase)printf("\n");
scanf("%d", &q);
while (q--)
{
scanf("%d%d", &r0, &c0);
printf("Cell data in (%d,%d) ", r0, c0);
if (simulate(&r0, &c0))printf("moved to (%d,%d)\n", r0, c0);
else printf("GONE\n");
}
}
return 0;
}
```

uDebug has passed, but the RE code is as follows:
If only dalao could help me to see it

```#include<stdio.h>
const int base = 10000;
int r, c, n, q, kase, a[100][100];
void ins(char type, int dis, int x)
{
if (type == 'R')
{
r++;
for (int i = r; i >= x + dis + 1; i--)
for (int j = 1; j <= c; j++)
a[i][j] = a[i - 1][j];
for (int j = 1; j <= c; j++)a[x + dis][j] = 0;
}
else
{
c++;
for (int j = c; j >= x + dis + 1; j--)
for (int i = 1; i <= r; i++)
a[i][j] = a[i][j - 1];
for (int i = 1; i <= r; i++)a[i][x + dis] = 0;
}
}
void del(char type, int dis, int x)
{
if (type == 'R')
{
for (int i = x - dis; i < r; i++)
for (int j = 1; j <= c; j++)
a[i][j] = a[i + 1][j];
r--;
}
else
{
for (int j = x - dis; j < c; j++)
for (int i = 1; i <= r; i++)
a[i][j] = a[i][j + 1];
c--;
}
}
void change()
{
char cmd[15];
scanf("%s", cmd);
if (cmd[0] == 'E')
{
int x1, y1, x2, y2, tmp;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
tmp = a[x1][y1], a[x1][y1] = a[x2][y2], a[x2][y2] = tmp;
}
else
{
int cnt, tmp;
scanf("%d", &cnt);
if (cmd[0] == 'I')
for (int i = 0; i < cnt; i++)
{
scanf("%d", &tmp);
ins(cmd[1], i, tmp);
}
else for (int i = 0; i < cnt; i++)
{
scanf("%d", &tmp);
del(cmd[1], i, tmp);
}
}
}
void find()
{
int x, y;
scanf("%d%d", &x, &y);
for (int i = 1; i <= r; i++)
for (int j = 1; j <= c; j++)
if (a[i][j] == x * base + y)
{
printf("Cell data in (%d,%d) moved to (%d,%d)\n", x, y, i, j);
return;
}
printf("Cell data in (%d,%d) GONE\n", x, y);
}
int main(void)
{
while (scanf("%d%d", &r, &c) == 2 && r)
{
if (kase)printf("\n");