给定行、列、从第几列开始,让求出步数。
这道题用模拟就可以了,不需要用深搜,因为路线是唯一的。
记得初始化就好。
#include <cstdio>
using namespace std;
struct Node
{
char a;
int flag;
int step;
};
int main()
{
int row,col,start;
Node Grid[15][15];
while((scanf("%d%d",&row,&col)) && row)
{
int i,j;
scanf("%d",&start);
for(i = 1; i <=row; i++) //初始化
{
getchar();
for(j = 1; j <= col; j++)
{
scanf("%c",&Grid[i][j].a);
Grid[i][j].flag = 0;
Grid[i][j].step = 0;
}
}
int Step = 0;
int r,c;
r = 1,c = start;
while(1)
{
if(r==0||r>row||c==0||c>col)
{
printf("%d step(s) to exit\n",Step);
break;
}
if(Grid[r][c].flag==1)
{
printf("%d step(s) before a loop of %d step(s)\n",Grid[r][c].step,Step-Grid[r][c].step);
break;
}
Grid[r][c].flag = 1;
Grid[r][c].step = Step;
if(Grid[r][c].a=='N')
{
r--;
}
else if(Grid[r][c].a=='S')
{
r++;
}
else if(Grid[r][c].a=='W')
{
c--;
}
else if(Grid[r][c].a=='E')
{
c++;
}
Step++;
}
}
return 0;
}