自动走迷宫_记录走的路径

dfs记录路径(非最简)
#include
using namespace std;
char p[1200][1200];
int f[1200][1200];
int step[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int temp=0,n,coutt=0;
struct kkk
{
int x,y;
}w[12000];
kkk q[12000];
void dfs(int xi,int yi)
{
q[coutt].x=xi;
q[coutt].y=yi;
if(temp==0)coutt++;
int xx,yy;
if(p[xi][yi]=='N'){temp=1;return ;}
for(int i=0;i<4;i++)
{
xx=xi+step[i][0];
yy=yi+step[i][1];
if(p[xx][yy]!='#'&&f[xx][yy]==0&&xx>0&&yy>0&&xx
{
f[xx][yy]=1;
dfs(xx,yy);
f[xx][yy]=0;
}
}
if(temp==0)coutt--;
return ;
}
int main()
{
int a,b;
cin>>n;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
cin>>p[i][j];
if(p[i][j]=='E')
{
a=i;b=j;
}
}
dfs(a,b);
if(temp==1)
cout<<"YES"<<endl;
else cout<<"NO"<<endl;
if(temp==1)
for(int i=0;i<=coutt-1;i++)
cout<<q[i].x<<' '<<q[i].y<<endl;
return 0;
}



bfs记录路径(最简)
#include
#include
using namespace std;
char p[1200][1200];
int f[1200][1200];
int head=0,end=2;
int step[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
struct kkk
{
int x,y;
char xs[12000],ys[12000];
}w[12000];
void bfs(int xi,int yi)
{
char xx,yy;
w[head+1].x=xi;
w[head+1].y=yi;
  while(head
  {
  head++;
  for(int i=0;i<4;i++)
  {
  xx=w[head].x+step[i][0];
  yy=w[head].y+step[i][1];
  if(p[xx][yy]=='.'&&f[xx][yy]==0)
  {
  f[xx][yy]=1;
w[end].x=xx;
w[end].y=yy;
strcpy(w[end].xs,w[head].xs);
strcpy(w[end].ys,w[head].ys); 
w[end].xs[strlen(w[end].xs)]='0'+xx;
w[end].ys[strlen(w[end].ys)]='0'+yy;
end++;
}
if(p[xx][yy]=='N')
{
cout<<"YES";
strcpy(w[end].xs,w[head].xs);
strcpy(w[end].ys,w[head].ys); 
w[end].xs[strlen(w[end].xs)]='0'+xx;
w[end].ys[strlen(w[end].ys)]='0'+yy;
return ;
}
  }
  }
  cout<<"NO";
  loop: ;
}
int main()
{
int n,a1,b1,a2,b2;
cin>>n;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
cin>>p[i][j];
if(p[i][j]=='E')
{
a1=i;b1=j;
}
if(p[i][j]=='N')
{
a2=i;b2=j;
}
}
bfs(a1,b1);
for(int i=0;i
cout<<w[end].xs[i]<<' '<<w[end].ys[i]<<endl;
cout<<a2<<' '<<b2<<endl;
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值