Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
Register

## 1A水过

Posted by xd560 at 2015-02-09 19:35:21 on Problem 1573
```/*
560
*/
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>

using namespace std;

const int maxn=500;
char map[maxn][maxn];
int vis[maxn][maxn];
int stp[maxn][maxn];
int n,m,st;

void go(int i,int j,int step)
{
if(vis[i][j]==0){
cout<<step-1<<" step(s) to exit"<<endl;
return;
}
if(vis[i][j]==1){
int pre=stp[i][j]-1;
int loop=step-stp[i][j];
cout<<pre<<" step(s) before a loop of "<<loop<<" step(s)"<<endl;
return;
}
vis[i][j]=1;
stp[i][j]=step;
switch(map[i][j]){
case 'N':i--;break;
case 'S':i++;break;
case 'E':j++;break;
case 'W':j--;break;
}
go(i,j,step+1);
}

int main()
{
while(cin>>n>>m>>st,n||m||st){
memset(vis,0,sizeof(vis));//0 wall
memset(stp,0,sizeof(stp));
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>map[i][j];
vis[i][j]=-1;
}
}
go(1,st,1);
}
return 0;
}
```

Followed by: