http://poj.org/problem?id=1573
没什么好说的。
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
char G[15][15];
int vis[15][15];
int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
#endif // Yinku
int n, m, ej;
while(~scanf("%d%d%d", &n, &m, &ej)) {
if(n == 0)
break;
for(int i = 1; i <= n; ++i) {
scanf("%s", G[i] + 1);
memset(vis[i], -1, sizeof(vis[i]));
}
int i = 1, j = ej;
int cnt = 0;
while(1) {
if(i < 1 || i > n || j < 1 || j > m) {
printf("%d step(s) to exit\n", cnt);
break;
}
if(vis[i][j] != -1) {
printf("%d step(s) before a loop of %d step(s)\n", vis[i][j], cnt - (vis[i][j]));
break;
}
vis[i][j] = cnt++;
if(G[i][j] == 'N') {
--i;
} else if(G[i][j] == 'S') {
++i;
} else if(G[i][j] == 'E') {
++j;
} else {
--j;
}
}
}
}