一个原来做过的模拟题,大早上做题就是头脑清楚啊。1Y..呵呵。。
View Code
#include#include #include #define maxn 107 using namespace std; char str[maxn][maxn]; bool visit[maxn][maxn];//标记是否已经走过 int map[maxn][maxn];//存储到达ij这点时走了多少步 int main() { int n,m,s,i,j; int a,b; bool flag; while (cin>>n>>m>>s) { if (!n && !m && !s) break; memset(visit,false,sizeof(visit)); flag = false; for (i = 0; i < n; ++i) scanf("%s",str[i]); i = 0; j = s - 1; int len = 1; map[i][j] = len++; visit[i][j] = true; while ((i >= 0 && i < n) && (j >=0 && j < m)) { if (str[i][j] == 'N') { i--; if (visit[i][j] == true) { a = map[i][j] - 1; b = map[i + 1][j] - map[i][j] + 1; flag = true; break; } visit[i][j] = true; map[i][j] = len++; } else if (str[i][j] == 'S') { i++; if (visit[i][j] == true) { a = map[i][j] - 1; b = map[i - 1][j] - map[i][j] + 1; flag = true; break; } visit[i][j] = true; map[i][j] = len++; } else if (str[i][j] == 'W') { j--; if (visit[i][j] == true) { a = map[i][j] - 1; b = map[i][j + 1] - map[i][j] + 1; flag = true; break; } visit[i][j] = true; map[i][j] = len++; } else if (str[i][j] == 'E') { j++; if (visit[i][j] == true) { a = map[i][j] - 1; b = map[i][j - 1] - map[i][j] + 1; flag = true; break; } visit[i][j] = true; map[i][j] = len++; } a = i; b = j; } if (flag) printf("%d step(s) before a loop of %d step(s)\n",a,b); else printf("%d step(s) to exit\n",map[a][b] - 1); } return 0; }