题意:给你一个地图,他有方向,你每次只能按照他的方向去走,问你能不能走出迷宫(就是超出边界),如果有环的话,就输出进入环的步数和环的大小
思路:没有思路,,,他让你咋办你就咋办呗 。。
上代码:
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
#include <queue>
#include <string>
#include <iostream>
using namespace std;
string ch[1111];
int vis[1111][1111],flag,n,m,k;
void dfs(int x,int y,int stept)
{
if(flag) return ;
if(x<0||y<0||x>=n||y>=m)
{
printf("%d step(s) to exit\n",stept);
flag = 1;
return ;
}
if(vis[x][y]!=-1)
{
printf("%d step(s) before a loop of %d step(s)\n",vis[x][y],stept - vis[x][y]);
flag = 1;
return ;
}
vis[x][y] = stept;
if(ch[x][y] == 'S') x++;
else if(ch[x][y] == 'E') y++;
else if(ch[x][y] == 'W') y--;
else if(ch[x][y] == 'N') x--;
dfs(x,y,stept+1);
}
int main()
{
while(cin>>n>>m>>k)
{
if(n == 0 && m == 0 && k ==0) break;
if(n == 0 ){
puts("0");continue;
}
memset(vis,-1,sizeof(vis));//我这里原来让vis都是0,但是我的stept开始也是0,所以会出现那种你一开始就有个环
flag = 0;
for(int i = 0 ; i < n ; i++)
{
cin>>ch[i];
}
k--;
dfs(0,k,0);
}
}