POJ3984 迷宫问题 —— BFS
题目链接:http://poj.org/problem?id=3984
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 24560 | Accepted: 14338 |
Description
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
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)
题解:
简单的BFS输出路径。
由于每个格子只能从一个格子转移过来(开始格子除外),所以开了fa[x][y][2]三维数组来存格子xy的上一个格子。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; struct node
{
int x, y;
};
int m[MAXN][MAXN], fa[MAXN][MAXN][], vis[MAXN][MAXN];
int dir[][] = { ,,,,-,,,- }; queue<node>que;
void bfs()
{
while(!que.empty()) que.pop();
node now, tmp;
now.x = now.y = ;
vis[][] = ;
que.push(now); while(!que.empty())
{
now = que.front();
que.pop(); if(now.x== && now.y==)
return;
for(int i = ; i<; i++)
{
tmp.x = now.x + dir[i][];
tmp.y = now.y + dir[i][];
if(tmp.x>= && tmp.x<= && tmp.y>= && tmp.y<= && !vis[tmp.x][tmp.y] && !m[tmp.x][tmp.y])
{
vis[tmp.x][tmp.y] = ;
fa[tmp.x][tmp.y][] = now.x;
fa[tmp.x][tmp.y][] = now.y;
que.push(tmp);
}
}
}
} void Print(int x, int y)
{
if(x!= || y!=)
Print(fa[x][y][], fa[x][y][]);
printf("(%d, %d)\n", x,y);
} int main()
{
for(int i = ; i<; i++)
for(int j = ; j<; j++)
scanf("%d",&m[i][j]); bfs();
Print(,);
}
POJ3984 迷宫问题 —— BFS的更多相关文章
- POJ-3984.迷宫问题(BFS + 路径输出)
昨天中午做的这道题,结果蛙了一整天,就因为一行代码困住了,今天算是见识到自己有多菜了.流泪.jpg 本题大意:给一个5 * 5的迷宫,1表示墙壁,0表示通路,从左上角走到右下角并输出路径. 本题思路: ...
- POJ3984 迷宫问题 BFS
看题传送门:http://poj.org/problem?id=3984 BFS水一发 明天帮学弟挑电脑顺便去玩.接下来几天好好看数据结构.嗯哼. 这题标准的BFS应用,唯一需要注意的是需要输出中间的 ...
- 2019年第十届蓝桥杯省赛-迷宫(BFS/Excel大法)
这题用dfs搜不出来,需要使用bfs并记录路径,设置好方向顺序跑就ok 正解类似:POJ-3984 迷宫问题 然而毕竟是暴力杯,我们的原则是代码能省就省(懒癌晚期 于是乎网上便出现了形形色色的题解,笔 ...
- Poj3984 迷宫问题 (BFS + 路径还原)
Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, ...
- POJ-3984 迷宫问题(BFS找最短路径并保存)
问题: 定义一个二维数组: 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, ...
- poj3984 迷宫问题(简单的输出路径的bfs)
题目链接 http://poj.org/problem?id=3984 中文题题意不解释了 反正就是简单的结构体套结构体存一下路径就行了 #include <iostream> #incl ...
- POJ-3984 迷宫问题 (BFS)
题意:有一个\(5\)X\(5\)的\(01\)图,输出从左上角走到右下角的最短路径. 题解:基础的bfs,这里困难的是如何输出这个最短路径,我们可以用一个结构体来存点和路径,我们每次向外去拓展的时候 ...
- POJ3984 迷宫问题【水BFS】
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u011775691/article/details/28050277 #include <cs ...
- poj3984迷宫问题 广搜+最短路径+模拟队列
转自:http://blog.csdn.net/no_retreats/article/details/8146585 定义一个二维数组: int maze[5][5] = { 0, 1, 0, ...
随机推荐
- Google解决跨域
1.添加 --disable-web-security --user-data-dir=D:\tmp 2.在D的根目录新建tmp文件夹
- 2017 ACM/ICPC Asia Regional Shenyang Online 记录
这场比赛全程心态爆炸…… 开场脑子秀逗签到题WA了一发.之后0贡献. 前期状态全无 H题想复杂了,写了好久样例过不去. 然后这题还是队友过的…… 后期心态炸裂,A题后缀数组理解不深,无法特判k = 1 ...
- Codeforces 848B Rooter's Song(分类+模拟)
题目链接 Rooter's Song 题意 有n个舞者站在x轴上或y轴上,每个人有不同的出发时间.x轴上的舞者垂直x轴正方向移动,y轴上的舞者垂直y轴正方向移动. 当x轴的舞者和y轴的舞者相遇时,他 ...
- PAT (Advanced Level) 1088. Rational Arithmetic (20)
简单题. 注意:读入的分数可能不是最简的.输出时也需要转换成最简. #include<cstdio> #include<cstring> #include<cmath&g ...
- springBoot 跨域处理
首先喝水不忘挖井人,博客参考:https://www.cnblogs.com/nananana/p/8492185.html 方式一:新增一个configration类 或 在Application中 ...
- 加密算法和MD5等散列算法的区别(转)
本文转自http://www.cnblogs.com/eternalwt/archive/2013/03/21/2973807.html 感谢作者 1.在软件开发的用户注册功能中常出现MD5加密这个概 ...
- spring启动时加载字典表数据放入map
import java.util.HashMap; import java.util.List; import org.springframework.beans.factory.annotation ...
- [转]Visual Studio 2012 编译错误【error C4996: 'scanf': This function or variable may be unsafe. 】的解决方案
原文地址:http://www.cnblogs.com/gb2013/archive/2013/03/05/SecurityEnhancementsInTheCRT.html 在VS 2012 中编译 ...
- [Testing] Config jest to test Javascript Application -- Part 1
Transpile Modules with Babel in Jest Tests Jest automatically loads and applies our babel configurat ...
- ajax请求后台交互json示例
ajax请求,首先需要服务器(首先你需要node) npm i -g http-server 其次,进入当前目录(默认服务器端口8080) http-server 点击进入:localhost:808 ...