6264:走出迷宫 (BFS,板子题)

31 篇文章 0 订阅
30 篇文章 0 订阅

描述
当你站在一个迷宫里的时候,往往会被错综复杂的道路弄得失去方向感,如果你能得到迷宫地图,事情就会变得非常简单。
假设你已经得到了一个n*m的迷宫的图纸,请你找出从起点到出口的最短路。

输入
第一行是两个整数n和m(1<=n,m<=100),表示迷宫的行数和列数。
接下来n行,每行一个长为m的字符串,表示整个迷宫的布局。字符’.‘表示空地,’#'表示墙,'S’表示起点,'T’表示出口。
输出
输出从起点到出口最少需要走的步数。
样例输入
3 3
S#T
.#.

样例输出
6
题目链接
思路:BFS板子
代码:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>
#include<iostream>
using namespace std;
struct note
{
    int x;
    int y;
    int f;
    int s;
};
int nest[4][2]={{0,1},{0,-1},{-1,0},{1,0}};
int sx,sy,ex,ey;
int flag;
int n,m;
const int N=105;
int book[N][N];
char mp[N][N];
queue <note> que;
void bfs()
{
    int tx,ty;
    note temp;
    temp.x=sx;
    temp.y=sy;
    temp.s=0;
    temp.f=0;
    que.push(temp);
    book[sx][sy]=1;
    while(!que.empty())
    {
        for(int k=0; k<4; k++)
        {
            temp=que.front();
            tx=nest[k][0]+temp.x;
            ty=temp.y+nest[k][1];
            if(tx>=1 && tx<=n && ty>=1 && ty<=m && book[tx][ty]==0)
            {
                if(mp[tx][ty]=='.')
                {
                    book[tx][ty]=1;
                    temp.x=tx;
                    temp.y=ty;
                    temp.s=temp.s+1;
                    que.push(temp);
                }
                if(tx==ex && ty==ey)
                {
                    printf("%d\n",temp.s+1);
                    return;
                }
            }
        }
        que.pop();
    }


}

int main()
{
    scanf("%d %d",&n,&m);
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            cin>>mp[i][j];
            if(mp[i][j]=='S')
            {
                sx=i;
                sy=j;
            }
            else if(mp[i][j]=='T')
            {
                ex=i;
                ey=j;
            }
        }
    }
    bfs();



    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java中使用BFS算法解决走迷宫问的示例代码: ```java import java.util.*; public class MazeBFS { static int[][] maze = {{0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 1, 1, 1, 0, 0}, {0, 1, 0, 0, 0, 0, 0, 0}, {0, 1, 1, 1, 0, 1, 1, 0}, {0, 1, 0, 0, 0, 0, 1, 0}, {0, 1, 1, 1, 1, 1, 1, 0}, {0, 0, 0, 0, 0, 0, 0, 0}}; static int[][] direction = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; public static void main(String[] args) { int startX = 1; int startY = 1; int endX = 5; int endY = 6; Queue<Node> queue = new LinkedList<>(); boolean[][] visited = new boolean[maze.length][maze[0].length]; Node startNode = new Node(startX, startY, 0); queue.offer(startNode); visited[startX][startY] = true; while (!queue.isEmpty()) { Node node = queue.poll(); int x = node.x; int y = node.y; int step = node.step; if (x == endX && y == endY) { System.out.println(step); break; } for (int i = 0; i < 4; i++) { int nextX = x + direction[i][0]; int nextY = y + direction[i][1]; if (nextX >= 0 && nextX < maze.length && nextY >= 0 && nextY < maze[0].length && maze[nextX][nextY] == 0 && !visited[nextX][nextY]) { queue.offer(new Node(nextX, nextY, step + 1)); visited[nextX][nextY] = true; } } } } static class Node { int x; int y; int step; public Node(int x, int y, int step) { this.x = x; this.y = y; this.step = step; } } } ``` 在上述代码中,我们使用了一个Node类来表示迷宫中的每个节点,其中包含了x和y坐标以及当前步数step。我们使用一个队列来进行BFS搜索,并且使用一个二维布尔数组visited来记录每个节点是否已经被访问过。 我们从起点开始,将其加入队列中,并将其visited值设为true。然后不断从队列中取出节点进行搜索,直到队列为空或者找到终点为止。对于每个节点,我们尝试向四个方向扩展,如果扩展出来的节点在迷宫范围内、不是墙、没有被访问过,就将其加入队列中,并将visited值设为true。 最终,如果搜索到了终点,就输出步数并停止搜索。如果队列为空仍然没有找到终点,说明终点不可达。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值