模拟蚂蚁的每一步即可,具体看代码实现
AC代码:
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
int m, n;
int map[105][105];
int xx[4] = {-1, 0, 1, 0}, yy[4] = {0, 1, 0, -1};
int main() {
while(scanf("%d %d", &m, &n) != EOF) {
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++)
scanf("%d", &map[i][j]);
int x, y, k;
char s[5];
scanf("%d %d %s %d", &x, &y, s, &k);
int po; //朝向
if(s[0] == 'U') po = 0;
else if(s[0] == 'R') po= 1;
else if(s[0] == 'D') po= 2;
else if(s[0] == 'L') po= 3;
while(k) {
if(map[x][y] == 0) {
if(po == 0) po = 3;
else po--;
}
else {
po = (po + 1) % 4;
}
map[x][y] = map[x][y]^1;
x += xx[po];
y += yy[po];
k--;
}
printf("%d %d\n", x, y);
}
return 0;
}
原文地址:http://blog.csdn.net/u014355480/article/details/44194589