BFS迷宫问题模型(具体模拟过程见《啊哈算法》)

题目描述与DFS模型走迷宫那篇一样。小哈被困在迷宫里,小哼解救小哈。

这里用BFS来写。BFS(广搜)与DFS(深搜)的区别就在于,DFS是“不撞南墙不回头”,一条路走到不能再走之后才会回到起始点,另开辟一条新的道路;而BFS是将道路层层扩展,走到一个点时会同时搜索附近能到达的点,同时进行。

这里先附上我最开始的代码(但编译出来是错的):

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<math.h>
#include<set>
#include<map>
using namespace std;
struct node
{
    int x,y,s;
};
int main()
{
    struct node q[5000];
    int a[1005][1005]={0},book[1005][1005]={0};
    int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
    int i,j,n,m,p,q1,tx,ty,flag=0;
    int head,tail;  //队列
    scanf("%d%d",&n,&m);
    scanf("%d%d",&p,&q1);
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    head=1;
    tail=1;
    q[head].x=1;
    q[head].y=1;
    q[head].s=0;
    tail++;
    book[1][1]=1;
    while(head<tail)
    {
        for(i=1;i<=3;i++)
        {
            tx=q[head].x+dir[i][0];
            ty=q[head].y+dir[i][1];
            if(tx>n||tx<1||ty>m||ty<1)
                continue;
            if(a[tx][ty]==0&&book[tx][ty]==0)
            {
                book[tx][ty]=1;
                q[tail].x=tx;
                q[tail].y=ty;
                q[tail].s=q[head].s+1;
                tail++;
            }
            if(tx==p&&ty==q1)
            {
                flag=1;
                break;
            }
        }
        if(flag==1)
            break;
        else
            head++;
    }
    printf("%d\n",q[head-1].s);  //这个时候tail指向队列的末尾
    return 0;
}

一开始是不能输出,把最开始的那些变量定义在主函数前竟然就可以进行输入了(我也不知道为啥)。然后可以输入之后,我尝试了这组数据,但输出是0,还是错的:

问了同学,她把我的输入输出格式给改了(cin  cout,但是我觉得这没什么影响呀);还有走四个方向的那个for循环,我 i  一开始从1开始,这样就只进行了三个方向的搜索,应该从i=0开始;最后是输出q[head].s,而不是head-1;最后正确代码如下:

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<math.h>
#include<set>
#include<map>
using namespace std;
struct node
{
    int x,y,s;
} ;
struct node q[5000];
int a[1005][1005]= {0};
int book[1005][1005]= {0}, dir[4][2]= {{0,1},{1,0},{0,-1},{-1,0}};
int main()
{
    int i,j,n,m,p,q1,tx,ty,flag=0;
    int head,tail;  //队列
    cin>>n>>m;
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=m; j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    cin>>p>>q1;
    head=1;
    tail=1;
    q[head].x=1;
    q[head].y=1;
    q[head].s=0;
    tail++;
    book[1][1]=1;
    while(head<tail)
    {
        for(i=0; i<=3; i++)
        {
            tx=q[head].x+dir[i][0];
            ty=q[head].y+dir[i][1];
            if(tx>n||tx<1||ty>m||ty<1)
                continue;
            if(a[tx][ty]==0&&book[tx][ty]==0)
            {
                book[tx][ty]=1;
                q[tail].x=tx;
                q[tail].y=ty;
                q[tail].s=q[head].s+1;
                tail++;
            }
            if(tx==p&&ty==q1)
            {
                flag=1;
                break;
            }
        }
        if(flag==1)
            break;
        else
            head++;
    }
    printf("%d\n",q[head].s+1);  //这个时候tail指向队列的末尾
    return 0;
}

 

转载于:https://www.cnblogs.com/programming123/p/10833757.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用BFS算法解决Python迷宫问题的代码示例: ```python from queue import Queue # 定义迷宫地图 maze = [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 1, 1, 1, 0, 1, 0], [0, 1, 1, 0, 1, 1, 1, 0, 1, 0], [0, 1, 1, 1, 1, 0, 0, 1, 1, 0], [0, 1, 0, 0, 0, 1, 1, 1, 1, 0], [0, 1, 1, 1, 0, 1, 1, 1, 1, 0], [0, 1, 0, 1, 1, 1, 0, 1, 1, 0], [0, 1, 0, 0, 0, 1, 0, 0, 1, 0], [0, 0, 1, 1, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ] # 定义四个方向 directions = [(0, 1), (0, -1), (1, 0), (-1, 0)] # 定义BFS算法 def bfs(maze, start, end): queue = Queue() queue.put(start) visited = set() visited.add(start) prev = dict() while not queue.empty(): cur = queue.get() if cur == end: print_path(prev, start, end) return for direction in directions: next_pos = (cur[0] + direction[0], cur[1] + direction[1]) if is_valid(maze, next_pos) and next_pos not in visited: queue.put(next_pos) visited.add(next_pos) prev[next_pos] = cur # 判断下一个位置是否合法 def is_valid(maze, pos): if pos[0] < 0 or pos[0] >= len(maze) or pos[1] < 0 or pos[1] >= len(maze[0]): return False if maze[pos[0]][pos[1]] == 0: return False return True # 输出路径 def print_path(prev, start, end): ltmp = [] node = end while node != start: ltmp.append(node) node = prev[node] ltmp.append(start) t_print(ltmp) # 输出函数 def t_print(ltmp): node = ltmp[-1] ltmp_s = [] while node in prev: ltmp_s.append(node) node = prev[node] ltmp_s.append(node) ltmp_s.reverse() for i in ltmp_s: print(i) # 测试 start = (1, 1) end = (8, 8) bfs(maze, start, end) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值