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

## 这个代码是哪里会出现死循环吗？为什么是超时了 ……

Posted by wsxcn1230 at 2012-07-04 10:15:39 on Problem 1573
```#include <iostream>
using namespace std;

int m,n,x,y,result1,result2;
char data [12][12];
int map[12][12];

void xh()	//处理环路
{

int a=x,b=y;

do
{
if(data[x][y]=='N') {--x;++result2;}
if(data[x][y]=='S') {++x;++result2;}
if(data[x][y]=='W') {--y;++result2;}
if(data[x][y]=='E') {++y;++result2;}
}while(!(a==x && b==y));

cout<<result1-result2<< " step(s) before a loop of "<<result2<<" step(s)"<<endl;

return ;
}

int main()
{
while(cin>>m>>n>>y && m)
{
x=0;	--y;	result1=1;	result2=0;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{
cin>>data[i][j];
map[i][j]=0;
}

map[x][y]=1;

while(true)
{
if(data[x][y]=='N')
{
if(x>0 && !map[x-1][y]){--x;++result1;map[x][y]=1;}			//移动到下一格
else if(x==0) {cout<<result1<<" step(s) to exit"<<endl;break;}		//已移动至边缘，输出步数
else {--x;xh();break;}
}
if(data[x][y]=='S')
{
if(x<m-1 && !map[x+1][y]){++x;++result1;map[x][y]=1;}
else if(x==m-1) {cout<<result1<<" step(s) to exit"<<endl;break;}
else {++x;xh();break;}
}
if(data[x][y]=='W')
{
if(y>0 && !map[x][y-1]){--y;++result1;map[x][y]=1;}
else if(y==0)  {cout<<result1<<" step(s) to exit"<<endl;break;}
else {--y;xh();break;}
}
if(data[x][y]=='E')
{
if(y<n-1 && !map[x][y+1]){++y;++result1;map[x][y]=1;}
else if(y==n-1) {cout<<result1<<" step(s) to exit"<<endl;break;}
else {++y;xh();break;}
}

}
}
return 0;
}```

Followed by: