一个RE,其他都是WA,完全看不出来哪里有问题了(撅望)
#include <cstdio>
#include <iostream>
#include <string>
bool vis[1001][51][51];
char map[51][51];
int r, c, n, sx, sy;
std::string order[1001];
void dfs(int x, int y, int step) {
if(step == n + 1) {
map[x][y] = '*';
// for(int i = 1; i <= r; i++) {
// for(int j = 1; j <= c; j++)
// printf("%c", map[i][j]);
// printf("\n");
// }
printf("\n");
return;
}
if(vis[step][x][y])
return;
vis[step][x][y] = 1;
if(order[step] == "WEST") {
int i = 0;
while(y - i >= 1) {
i++;
if(map[x][y - i] != 'X') {
// printf("north:%d\n", i);
dfs(x, y - i, step + 1);
// i--;
}
else {
i--;
if(i == 0)
return;
dfs(x, y - i, step + 1);
break;
}
}
}
if(order[step] == "EAST") {
int i = 0;
while(y + i <= r) {
i++;
if(map[x][y + i] != 'X') {
// printf("south:%d\n", i);
dfs(x, y + i, step + 1);
}
else {
i--;
if(i == 0)
return;
dfs(x, y + i, step + 1);
break;
}
}
}
if(order[step] == "NORTH") {
int i = 0;
while(x - i >= 1) {
i++;
if(map[x - i][y] != 'X') {
// printf("west:%d\n", i);
dfs(x - i, y, step + 1);
}
else {
i--;
if(i == 0)
return;
dfs(x - i, y, step + 1);
break;
}
}
}
if(order[step] == "SOUTH") {
int i = 0;
while(x + i <= c) {
i++;
if(map[x + i][y] != 'X') {
// printf("east:%d\n", i);
dfs(x + i, y, step + 1);
}
else {
i--;
if(i == 0)
return;
dfs(x + i, y, step + 1);
break;
}
}
}
}
int main() {
scanf("%d%d", &r, &c);
for(int i = 1; i <= r; i++)
scanf("%s", map[i] + 1);
scanf("%d", &n);
for(int i = 1; i <= n; i++)
std::cin >> order[i];
for(int i = 1; i <= r; i++)
for(int j = 1; j <= c; j++)
if(map[i][j] == '*') {
map[i][j] = '.', sx = i, sy = j;
break;
}
dfs(sx, sy, 1);
for(int i = 1; i <= r; i++) {
for(int j = 1; j <= c; j++)
printf("%c", map[i][j]);
printf("\n");
}
return 0;
}