这是道模拟题, 紫书上两种做法一个是边操作边处理, 最后的结果就是改变后的表格, 第二个是把操作全都存起来, 直接输出某一个的位置。(紫书上有, 我就不废话了)
我是把整张表的改变情况算了出来, 开了个二维数组, 左边是初始位置, 里面是当前位置, 然后按照操作一个个得来。
当时不明白已经没了的位置和还存在的位置交换怎么处理, 后来试了几次发现存在的位置应该到那个没存在的位置。
我写了个测试的代码, 可以用来对拍。(对拍程序:http://blog.youkuaiyun.com/error/404.html?from=http%3a%2f%2fblog.youkuaiyun.com%2fxuziye0327%2farticle%2fdetails%2f44017623)
寒假做的题都没有发, 我会陆续发出来的。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#define MAXN 55
using namespace std;
int r, c, t, DR[MAXN];
struct table {
int r, c;
} T[MAXN][MAXN];
int main() {
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
freopen("my.out", "w", stdout);
#endif // ONLINE_JUDGE
int Case = 0;
while(cin >> r >> c >> t&& r) {
memset(T, 0, sizeof(T));
if(Case++) printf("\n");
for(int i = 1; i <= r; i++)
for(int j = 1; j <= c; j++) {
T[i][j].r = i;
T[i][j].c = j;
}
while(t--) {
char cmd[5];
cin >> cmd;
if(cmd[0] == 'E') {
bool ok1 = false, ok2 = false;
int r1, c1, r2, c2, t_r1, t_c1;
cin >> r1 >> c1 >> r2 >> c2;
for(int i = 1; i <= r; i++)
for(int j = 1; j <= c; j++) {
if(T[i][j].r == r1&& T[i][j].c == c1&& !ok1) {
ok1 = true;
r1 = i;
c1 = j;
}
if(T[i][j].r == r2&& T[i][j].c == c2&& !ok2) {
ok2 = true;
r2 = i;
c2 = j;
}
}
if(ok1&& ok2) {
t_r1 = T[r1][c1].r;
t_c1 = T[r1][c1].c;
T[r1][c1].r = T[r2][c2].r;
T[r1][c1].c = T[r2][c2].c;
T[r2][c2].r = t_r1;
T[r2][c2].c = t_c1;
} else if(ok1) {
T[r1][c1].r = r2;
T[r1][c1].c = c2;
} else if(ok2) {
T[r2][c2].r = r1;
T[r2][c2].c = c1;
}
} else {
int times;
cin >> times;
for(int i = 0; i < times; i++)
cin >> DR[i];
if(cmd[0] == 'D') {
for(int i = 1; i <= r; i++)
for(int j = 1; j <= c; j++)
for(int k = 0; k < times; k++) {
if(cmd[1] == 'R') {
if(T[i][j].r == DR[k]) {
T[i][j].r = 0;
T[i][j].c = 0;
}
} else {
if(T[i][j].c == DR[k]) {
T[i][j].r = 0;
T[i][j].c = 0;
}
}
}
for(int i = 1; i <= r; i++) {
for(int j = 1; j <= c; j++) {
int del = 0;
for(int k = 0; k < times; k++) {
if(cmd[1] == 'R') {
if(T[i][j].r > DR[k]) del++;
} else {
if(T[i][j].c > DR[k]) del++;
}
}
if(cmd[1] == 'R') T[i][j].r -= del;
else T[i][j].c -= del;
}
}
} else {
for(int i = 1; i <= r; i++)
for(int j = 1; j <= c; j++) {
int del = 0;
for(int k = 0; k < times; k++) {
if(cmd[1] == 'R') {
if(T[i][j].r >= DR[k]) del++;
} else {
if(T[i][j].c >= DR[k]) del++;
}
}
if(cmd[1] == 'R') T[i][j].r += del;
else T[i][j].c += del;
}
}
}
}
cin >> t;
printf("Spreadsheet #%d\n", Case);
while(t--) {
int x, y;
cin >> x >> y;
printf("Cell data in (%d,%d) ", x, y);
if(T[x][y].r&& T[x][y].c) printf("moved to (%d,%d)\n", T[x][y].r, T[x][y].c);
else printf("GONE\n");
}
}
return 0;
}
测试代码:
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <algorithm>
using namespace std;
int hehe[100];
int main() {
freopen("test.in", "w", stdout);
srand((unsigned)time(NULL));
int T = rand() % 5 + 1;
for(int i = 0; i < T; i++) {
int r = rand() % 50 + 1;
int c = rand() % 50 + 1;
int t;
t = rand() % 10 + 1;
printf("%d %d\n", r, c);
printf("%d\n", t);
for(int j = 0; j < t; j++) {
memset(hehe, 0, sizeof(hehe));
int s = rand() % 3 + 1;
if(s == 1) {
printf("EX ");
printf("%d %d %d %d", rand() % r + 1, rand() % c + 1,
rand() % r + 1, rand() % c + 1);
} else if(s == 2) {
printf("D");
int b = rand() % 2;
if(b) printf("R");
else printf("C");
int a = rand() % min(r, c) + 1;
printf(" %d ", a);
for(int k = 0; k < a; k++) {
int d;
if(b) d = rand() % r + 1;
else d = rand() % c + 1;
if(!hehe[d]) printf("%d ", d);
else {
int biogj;
if(b) biogj = r;
else biogj = c;
for(int p = 1; p <= biogj; p++)
if(!hehe[p]) {
printf("%d ", p);
hehe[p] = 1;
break;
}
}
hehe[d] = 1;
}
} else {
printf("I");
int b = rand() % 2;
if(b) printf("R");
else printf("C");
int a = rand() % min(r, c) + 1;
printf(" %d ", a);
for(int k = 0; k < a; k++) {
int d;
if(b) d = rand() % r + 1;
else d = rand() % c + 1;
if(!hehe[d])printf("%d ", d);
else {
int biogj;
if(b) biogj = r;
else biogj = c;
for(int p = 1; p <= biogj; p++)
if(!hehe[p]) {
printf("%d ", p);
hehe[p] = 1;
break;
}
}
hehe[d] = 1;
}
}
printf("\n");
}
t = rand() % 10 + 1;
printf("%d\n", t);
for(int j = 0; j < t; j++)
printf("%d %d\n", rand() % r + 1, rand() % c + 1);
}
printf("0 0\n");
return 0;
}