bfs迷宫

链接:https://ac.nowcoder.com/acm/contest/338/B
Sleeping is a favorite of little bearBaby, because the wetness of Changsha in winter is too uncomfortable. One morning, little bearBaby accidentally overslept. The result of being late is very serious. You are the smartest artificial intelligence. Now little bearBaby  asks you to help him figure out the minimum time it takes to reach the teaching building.
The school map is a grid of n*m, each cell is either an open space or a building (cannot pass), and the bedroom of little bearBaby is at (1,1)—— the starting point coordinates.The teaching building is at (x, y)——the target point coordinates, he  can only go up, down, left or right, it takes 1 minute for each step. The input data ensures that the teaching building is reachable.

链接:https://ac.nowcoder.com/acm/contest/338/B
来源:牛客网

输入描述:

The first line has two positive integers n, m , separated by spaces(1 <= n, m <= 100), n for the row, m for the columnNext there are two positive integers x, y, separated by spaces(1 <= x <= n, 1 <= y <= m) indicating the coordinates of the teaching buildingNext is a map of n rows and m columns, 0  indicate a open space and 1  indicate a obstacles.

输出描述:

For each test case, output a single line containing an integer giving the minimum time little bearBaby takes to reach the teaching building, in minutes.

示例1

输入

复制

5 4
4 3
0 0 1 0
0 0 0 0
0 0 1 0
0 1 0 0
0 0 0 1

输出

复制

7

说明

For the input example, you could go like this:(1,1)-->(1,2)-->(2,2)-->(2,3)-->(2,4)-->(3,4)-->(4,4)-->(4,3),so the minimum time is 7.

备注:

First grid in the upper left corner is(1,1)

AC代码:
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
inline int read() {int x=0,f=1;char c=getchar();while(c!=‘-‘&&(c<‘0‘||c>‘9‘))c=getchar();if(c==‘-‘)f=-1,c=getchar();while(c>=‘0‘&&c<=‘9‘)x=x*10+c-‘0‘,c=getchar();return f*x;}
typedef long long ll;
const int maxn = 1e4+10;
struct node{
    int x,y;
    int st;
};

int dir[4][2]={-1,0,1,0,0,-1,0,1};
int vis[maxn][maxn];
int n,m,n1,m1;
char a[1000][1000];

int bfs(int u,int v){
    node tmp,nex;
    tmp.x=u;
    tmp.y=v;
    tmp.st=0;
    queue<node>q;
    q.push(tmp);
    while(!q.empty()){
        tmp=q.front();
        q.pop();
        for(int i=0;i<4;i++){
            nex.x=tmp.x+dir[i][0];
            nex.y=tmp.y+dir[i][1];
            nex.st=tmp.st+1;
            if(nex.x<0||nex.y<0||nex.x>=n||nex.y>=m||a[nex.x][nex.y]==‘1‘||vis[nex.x][nex.y]==1)continue;
            vis[nex.x][nex.y]=1;
            if(nex.x==n1-1&&nex.y==m1-1){
                return nex.st;
            }

            q.push(nex);
        }
    }
}
int main()
{

    cin>>n>>m>>n1>>m1;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cin>>a[i][j];
        }
    }
    int ans=bfs(0,0);
    printf("%d",ans);
    return 0;
}

原文地址:https://www.cnblogs.com/lipu123/p/12177507.html

时间: 2024-08-13 20:52:15

bfs迷宫的相关文章

uva 816 BFS迷宫

这是一道比较复杂的BFS迷宫问题,状态由普通的迷宫问题的坐标(x,y)变为三个变量的状态(r,c,dir)其中dir是到达(r,c)两点的方向,这个变量非常重要,导致了这题比普通的BFS迷宫问题要更加复杂. 普通BFS解法 http://blog.csdn.net/iboxty/article/details/45888923 BFS是用队列实现的,很重要并且要理解的是:每一个节点只访问一次,而且每次BFS过程都保证压入队列中的节点到起点的距离是最短的,这题也有这样的思想. #include <

uva 816 - Abbott&#39;s Revenge(有一点难度的bfs迷宫题目)

就是典型的bfs,但这道题目的难点在于其条件的读取和判断并不简单,需要想办法来读取条件,也需要想办法来判断在每个点处能不能满足向下继续走的条件. #include<cstdio> #include<cstring> #include<string> #include<queue> #include<iostream> #include<algorithm> using namespace std; struct note { int

BFS迷宫问题

1 #include<graphics.h> 2 #include<stdlib.h> 3 #include<conio.h> 4 #include<time.h> 5 #include<vector> 6 #include<queue> 7 #include<stack> 8 #include<iostream> 9 #include <algorithm> 10 11 using namespa

3299: [USACO2011 Open]Corn Maze玉米迷宫

3299: [USACO2011 Open]Corn Maze玉米迷宫 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 137  Solved: 59[Submit][Status][Discuss] Description 今年秋天,约翰带着奶牛们去玩玉米迷宫.迷宫可分成NxM个格子,有些格子种了玉 米,种宥玉米的格子无法通行. 迷宫的四条边界上都是种了玉米的格子,其屮只有一个格子 没种,那就是出口. 在这个迷宫里,有一些神奇的传送点6每个传送点

(BFS)poj2935-Basic Wall Maze

题目地址 题目与最基本的BFS迷宫的区别就是有一些障碍,可以通过建立三维数组,标记某个地方有障碍不能走.另一个点是输出路径,对此建立结构体时要建立一个pre变量,指向前一个的下标.这样回溯(方法十分经典)就可以顺利的输出. 这道题难度的确很小,可是我却花了近两个小时才顺利AC,实在是现在水平太不足了,要努力学习的真的是有好多啊.不管怎样,尽力吧. 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4

BFS学习总结

BFS学习总结 给你一个n*m的网格迷宫,迷宫中有些格子不能走,其他的格子都能走.然后给你起点与终点,问你从起点走到终点最少需要多少步? 上面的问题就是一个典型的BFS问题,对于这类问题来说,只要你掌握了这类问题的关键思想,其实他们都是可以用类似的思路来做的. 你可以把BFS问题想象成:从一个父亲(起点状态)生儿子(后继状态),儿子又生孙子(后继状态)的过程,只要这个家族中出生了一个满意的后代(终点状态),这个家族就不生了. 但是如果这个家族中有两个完全一样的人出生(他们的辈分不一定相同),那么

ACM/ICPC 之 BFS-简单障碍迷宫问题(POJ2935)

题目确实简单,思路很容易出来,难点在于障碍的记录,是BFS迷宫问题中很经典的题目了. POJ2935-Basic Wall Maze 题意:6*6棋盘,有三堵墙,求从给定初始点到给定终点的最短路,输出同一路长的最短路中的任一路径. 题解:BFS就不说了,对于障碍的记录,我的想法是针对每一个点都记录一次各方向上的情况.比如东边有障碍则在障碍两侧的点增加一个方向数组,用以记录左点的东侧和右点的西侧有障碍,在BFS扩展该方向时,增加一层循环判断一次该方向是否有障碍就行,时间度不会耗费很高,最坏时间度也

BFS 基础

做一道题复习一下BFS迷宫= http://hncu.acmclub.com/index.php?app=problem_title&id=111&problem_id=1102 题目描述 小明置身于一个迷宫,请你帮小明找出从起点到终点的最短路程. 小明只能向上下左右四个方向移动. 输入格式 输入包含多组测试数据.输入的第一行是一个整数T,表示有T组测试数据. 每组输入的第一行是两个整数N和M(1<=N,M<=100). 接下来N行,每行输入M个字符,每个字符表示迷宫中的一个小

迷宫问题求解——C++

迷宫问题思路 根据昨天的博客,有如下几种解决方案 克鲁斯卡尔 ,为避免死循环,需要设定优化路径的次数. Prim,为避免死循环,需要设定优化路径的次数,暂定200次. BFS , 实现简单,无死循环. DFS , 实现简单,无死循环,复杂度较低. 动态规划,实时根据权重调整方向,目前看来最合适的解决方案.需要以数据证明. 综上,本次优先选择BFS,首先不存在死循环的风险,其次算法复杂度较低,容易理解且实现.适合初步练手. 一. 思路及程序算法 首先建立迷宫,将其看作点位矩阵,先把墙堆起来,剩下的