做水题找感觉,一次AC水过的
#include <iostream>
#include <cstdio>
using namespace std;
int row,column,start;
int map[11][11];
char dir[11][11];
int step;
bool flag;
int before;
void DFS(int x,int y)
{
if(map[x][y] != 0)
{
flag = true;
before = map[x][y];
return;
}
step++;
map[x][y] = step;
int xx,yy;
xx = x;
yy = y;
if(dir[x][y] == 'W')
{
yy = y - 1;
}
else if(dir[x][y] == 'E')
{
yy = y + 1;
}
else if(dir[x][y] == 'N')
{
xx = x - 1;
}
else if(dir[x][y] == 'S')
{
xx = x + 1;
}
else
{
cout<<"error"<<endl;
}
if(xx >= 0 && xx <= row -1 && yy >= 0 && yy <= column - 1)
{
DFS(xx,yy);
}
return;
}
int main()
{
while(cin>>row>>column)
{
if(row == 0 || column == 0)
{
break;
}
cin>>start;
for(int i = 0; i < row; i++)
{
for(int j = 0; j < column; j++)
{
cin>>dir[i][j];
map[i][j] = 0;
}
}
step = 0;
flag = false;
DFS(0,start-1);
if(flag == false)
{
cout<<step<<" step(s) to exit"<<endl;
}
else
{
cout<<before-1<<" step(s) before a loop of "<<step-before+1<<" step(s)"<<endl;
}
}
return 0;
}