输入五维数组走迷宫

输入五维数组走迷宫


#include<stdio.h>

#include<string.h>
#include<queue>
#include<stdlib.h>
using namespace std;
int map[7][7];
int vis[7][7];     
int d[4][2]={1,0,0,1,0,-1,-1,0};      
struct node{
int x,y;
int step;    
node *last;  
};
node* Bfs(int x,int y){
int i;
memset(vis,0,sizeof(vis));
vis[0][0]=1;
queue<node *>q;
node *s;
s=(node *)malloc(sizeof(node));
s->x=x;
s->y=y;
s->step=0;
s->last=NULL;
q.push(s);
while(!q.empty())
{
s=q.front();
q.pop();
if(s->x==4&&s->y==4)
return s;
for(i=0;i<4;i++)
{
int xx=s->x+d[i][0];
int yy=s->y+d[i][1];
if(xx<0||yy<0||xx>4||yy>4)continue;
if(map[xx][yy])continue;
if(vis[xx][yy])continue;
vis[xx][yy]=1;
node *e;       //注意e必须定义在循环里面
e=(node *)malloc(sizeof(node));
e->x=xx;
e->y=yy;
e->step=s->step+1;
e->last=s;
q.push(e);
}
}
return NULL;
}
void Output(node *s)
{    //递归地输出路径
if(s==NULL)return;
Output(s->last);
printf("(%d, %d)\n",s->x,s->y);
}
int main()
{
int i,j;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
scanf("%d",&map[i][j]);
}
}
Output(Bfs(0,0));
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值