得分 | 100 |
CPU使用 | 0ms |
内存使用 | 996.0KB |
试题名称 | 历届试题 兰顿蚂蚁 |
语言 | C++ |
画出状态图,然后打表
代码:
#include <iostream>
#include <cstdio>
using namespace std;
int a[102][102];
int map[100][2][2];
int k;
void dfs(int x, int y, char s, int step){
if (step == k){
printf("%d %d\n", x, y);
return;
}
//printf("%d %d %c %d\n", x, y, s, step);
if (s == 'U'){
if (a[x][y] == 0){
a[x][y] = 1;
x += map[s][0][0];
y += map[s][0][1];
s = 'L';
}else{
a[x][y] = 0;
x += map[s][1][0];
y += map[s][1][1];
s = 'R';
}
dfs(x, y, s, step + 1);
}else if(s == 'L'){
if (a[x][y] == 0){
a[x][y] = 1;
x += map[s][0][0];
y += map[s][0][1];
s = 'D';
}else{
a[x][y] = 0;
x += map[s][1][0];
y += map[s][1][1];
s = 'U';
}
dfs(x, y, s, step + 1);
}else if(s == 'D'){
if (a[x][y] == 0){
a[x][y] = 1;
x += map[s][0][0];
y += map[s][0][1];
s = 'R';
}else{
a[x][y] = 0;
x += map[s][1][0];
y += map[s][1][1];
s = 'L';
}
dfs(x, y, s, step + 1);
}else if (s == 'R'){
if (a[x][y] == 0){
a[x][y] = 1;
x += map[s][0][0];
y += map[s][0][1];
s = 'U';
}else{
a[x][y] = 0;
x += map[s][1][0];
y += map[s][1][1];
s = 'D';
}
dfs(x, y, s, step + 1);
}
}
int main(){
int m, n, x, y;
char s;
scanf("%d%d", &m, &n);
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
scanf("%d", &a[i][j]);
scanf("%d%d%c%c%d", &x, &y, &s, &s, &k);
map['U'][0][0] = 0;
map['U'][0][1] = -1;
map['U'][1][0] = 0;
map['U'][1][1] = 1;
//map['L'] = {{1, 0}, {-1, 0}};
map['L'][0][0] = 1;
map['L'][0][1] = 0;
map['L'][1][0] = -1;
map['L'][1][1] = 0;
//map['D'] = {{0, 1}, {0, -1}};
map['D'][0][0] = 0;
map['D'][0][1] = 1;
map['D'][1][0] = 0;
map['D'][1][1] = -1;
//map['R'] = {{-1, 0}, (1, 0)};
map['R'][0][0] = -1;
map['R'][0][1] = 0;
map['R'][1][0] = 1;
map['R'][1][1] = 0;
dfs(x, y, s, 0);
return 0;
}