迷宫问题

定义一个二维数组:

int maze[5][5] = {

	0, 1, 0, 0, 0,

	0, 1, 0, 1, 0,

	0, 0, 0, 0, 0,

	0, 1, 1, 1, 0,

	0, 0, 0, 1, 0,

};


它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。Input

一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。

Output

左上角到右下角的最短路径,格式如样例所示。

Sample Input

0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0

Sample Output

(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)

 

 

代码:(初级)

#include<iostream>
#include<cstring>
using namespace std;
struct node
{
    int xi,yi,HeaderNum;
} q[10000];
int a[30][30];
int visited[30][30];
void print(int t1)
{
    if(t1!=-1)
    {
        print(q[t1].HeaderNum);
        cout<<"("<<q[t1].xi<<", "<<q[t1].yi<<")"<<endl;
    }
}
int main()
{
    memset(visited,0,sizeof(visited));
    for(int i=0; i<5; i++)
        for(int j=0; j<5; j++)
            cin>>a[i][j];
    int second=1;
    q[1].xi=0;
    q[1].yi=0;
    q[1].HeaderNum=-1;
    visited[0][0]=1;
    for(int first=1;first<20; first++)
    {
        int temp1=q[first].xi;
        int temp2=q[first].yi;
        if(temp1==4&&temp2==4)
        {
            print(first);
            break;
        }
       // cout<<"**"<<visited[temp1+1][temp2]<<" "<<a[temp1+1][temp2]<<endl;
        if(temp1+1<5&&visited[temp1+1][temp2]==0&&a[temp1+1][temp2]==0)
        {

            visited[temp1+1][temp2]=1;
            q[++second].HeaderNum=first;
            q[second].xi=temp1+1;
            q[second].yi=temp2;
        }
        if(temp1-1>=0&&visited[temp1-1][temp2]==0&&a[temp1-1][temp2]==0)
        {
            visited[temp1-1][temp2]=1;
            q[++second].xi=temp1-1;
            q[second].yi=temp2;
            q[second].HeaderNum=first;
        }
       if(temp2+1<5&&visited[temp1][temp2+1]==0&&a[temp1][temp2+1]==0)
        {
            visited[temp1][temp2+1]=1;
            q[++second].xi=temp1;
            q[second].yi=temp2+1;
            q[second].HeaderNum=first;
        }
        if(temp2-1>=0&&visited[temp1][temp2-1]==0&&a[temp1][temp2-1]==0)
        {
            visited[temp1][temp2-1]=1;
            q[++second].xi=temp1;
            q[second].yi=temp2-1;
            q[second].HeaderNum=first;
        }
    }
    return 0;

}
 

暂存:

#include<iostream>
#include<cstring>
#include<string>
using namespace std;
int mapp[15][15];
int visited[15][15];
int Road[15][15];
struct node
{
    int Head;
    int xi,yi;
} q[100];
int f[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};
void Print(int t)
{
    if(t!=-1)
    {
        Print(q[t].Head);
        cout<<"("<<q[t].xi<<","<<q[t].yi<<")"<<endl;
    }
}
void Bfs(int t1,int t2)
{
    memset(visited,0,sizeof(visited));
    visited[t1][t2]=1;
    int first=-1,second=1;
    q[second].xi=t1;
    q[second].yi=t2;
    q[second].Head=first;
    first=0;
    while(1)
    {
    first++;
        if(q[first].xi==4&&q[first].yi==4)
        {
            Print(first);
            return ;
        }
        int x=q[first].xi;
        int y=q[first].yi;
        for(int i=0; i<4; i++)
        {
            int xi=x+f[i][0];
            int yi=y+f[i][1];
            if(xi<0||xi>=5||yi<0||yi>=5)continue;
            if(visited[xi][yi]==1)continue;
            if(mapp[xi][yi]==1)continue;
            visited[xi][yi]=1;
            q[++second].xi=xi;
            q[second].yi=yi;
            q[second].Head=first;
        }
    }
}
int main()
{
    for(int i=0; i<5; i++)
    {
        for(int j=0; j<5; j++)
        {
            cin>>mapp[i][j];
        }
    }
    Bfs(0,0);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值