题目链接:http://acm.hust.edu.cn/vjudge/contest/124435#problem/J
密码:acm
Sample Input NEESWE
WWWESS
SNWWWW SESWE
EESNW
NWEEN
EWSEN Sample Output
step(s) to exit
step(s) before a loop of step(s)
分析:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
#include<queue>
#include<math.h>
using namespace std; #define N 1001
char s[N][N];
int a[N][N],b[N][N]; int main()
{
int n,m,c,ans;
while(scanf("%d%d%d",&n,&m,&c),n+m+c)
{
memset(a,,sizeof(a));
memset(b,,sizeof(b));
ans=; for(int i=;i<=n;i++)
scanf("%s",s[i]); int i=;
int j=c-;///字符串里面从零开始记录的,老是忘~~~~(>_<)~~~~
while(1)
{
b[i][j]=;
if(s[i][j]=='N')
i=i-;
else if(s[i][j]=='S')
i=i+;
else if(s[i][j]=='E')
j=j+;
else
j=j-;
ans++;
if(b[i][j])
{
printf("%d step(s) before a loop of %d step(s)\n",a[i][j],ans-a[i][j]);
break;
}
a[i][j]=ans; if(i==||j==-||i==n+||j==m)
{
printf("%d step(s) to exit\n",ans);
break;
}
}
}
return ;
}