POJ.3894 迷宫问题 (BFS+记录路径)
POJ.3894 迷宫问题 (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, 0, 1, 0,
};
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。
简单的BFS,路径记录开一个二维数组就好。
代码总览
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#define nmax 6
using namespace std;
int mp[nmax][nmax];
typedef struct{
int x;
int y;
bool isvis;
}mes;
typedef struct{
int x;
int y;
}point;
mes visit[nmax][nmax];
int spx[5] = {0,1,0,-1};
int spy[5] = {1,0,-1,0};
bool check(point temp)
{
if(temp.x <0 || temp.x >=5 ||temp.y < 0 || temp.y >=5 || visit[temp.x][temp.y].isvis|| mp[temp.x][temp.y] == 1)
return false;
else return true;
}
void bfs()
{
memset(visit,0,sizeof(visit));
queue<point> q;
while(!q.empty()) q.pop();
point temp = {0,0},head;
visit[temp.x][temp.y].isvis = true;
q.push(temp);
while(!q.empty()){
head = q.front(); q.pop();
if(head.x == 4 && head.y == 4){
return;
}
for(int i = 0;i<4;++i){
temp.x = head.x + spx[i];
temp.y = head.y + spy[i];
if(check(temp)){
visit[temp.x][temp.y].isvis = true;
visit[temp.x][temp.y].x = head.x;
visit[temp.x][temp.y].y = head.y;
q.push(temp);
}
}
}
}
void output(point temp)
{
point h;
vector<point> v; v.clear();
while(1){
v.push_back(temp);
if(temp.x == 0 && temp.y == 0) break;
h.x = visit[temp.x][temp.y].x;
h.y = visit[temp.x][temp.y].y;
temp = h;
}
for(int i = v.size()-1;i>=0;--i){
printf("(%d, %d)\n",v[i].x,v[i].y);
}
}
int main()
{
for(int i = 0;i<5;++i){
for(int j = 0;j<5;++j){
scanf("%d",&mp[i][j]);
}
}
bfs();
output({4,4});
return 0;
}
POJ.3894 迷宫问题 (BFS+记录路径)的更多相关文章
- poj 3414 Pots 【BFS+记录路径 】
//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...
- (简单) POJ 3414 Pots,BFS+记录路径。
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- Codeforces-A. Shortest path of the king(简单bfs记录路径)
A. Shortest path of the king time limit per test 1 second memory limit per test 64 megabytes input s ...
- HDU1026--Ignatius and the Princess I(BFS记录路径)
Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has ...
- - 迷宫问题 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, 0, 1, ...
- 迷宫问题(bfs+记录路径)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105278#problem/K K - 迷宫问题 Time Limit:1000 ...
- hdu 1026 Ignatius and the Princess I (bfs+记录路径)(priority_queue)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1026 Problem Description The Princess has been abducted ...
- hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)
以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要 ...
- poj 3984 迷宫问题 bfs
学会这道水题之后我懂得了不少哈,首先水题也能学到不少知识,尤其像我这样刚入门的小菜鸟,能学到一些小技巧. 然后就是可以从别人的代码里学到不一样的思路和想法. 这题就是求最短的路径,首先想到就是用bfs ...
随机推荐
- 搜索引擎ElasticSearch系列(三): ElasticSearch2.4.4 bigdesk插件安装
一:ElasticSearch bigdesk插件简介 bigdesk是elasticsearch的一个集群监控工具,可以通过它来查看es集群的各种状态,如:cpu.内存使用情况,索引数据.搜索情况, ...
- JMeter与WireShark
最近在学习JMeter,刚学了一点皮毛,就掉入了WireShark的坑,我发现在学习的道路上就是不断的给自己挖坑,之前在学习LoadRunner的道路上,遇到的坑更大,就单纯的安装LR就耗费了两个星期 ...
- Educational Codeforces Round 63 D. Beautiful Array
D. Beautiful Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 基于C#的机器学习--面部和动态检测-图像过滤器
在本章中,我们将展示两个独立的例子,一个用于人脸检测,另一个用于动态检测,以及如何快速地将这些功能添加到应用程序中. 在这一章中,我们将讨论: 面部检测 动态检测 将检测添加到应用程序中 面部检测 人 ...
- Leetcode_2. Add_Two_Number
2. Add_Two_Number 用两个非空链表分别表示两个非负整数,链表的节点表示数字的位,链表头表示数字的低位,链表尾表示数字高位.求两个链表所表示数字的和. 比如: Input: (2 -&g ...
- 亚马逊6月18日发布惊世之作 或为3D智能手机
亚马逊将在 6 月 18 日举行一个产品发布会. 其内容可能是关于传闻已久的亚马逊智能手机.该公司在 YouTube 上公布了一段炫耀这款设备的视频.这段视频展示了很多人在这款产品前摇头晃脑,并且表现 ...
- VIM字符编码基础知识
1 字符编码基础知识 字符编码是计算机技术中最基本和最重要的知识之一.如果缺乏相关知识,请自行恶补之.这里仅做最简要的说明. 1.1 字符编码概述 所谓的字符编码,就是对人类发明的每一个文字进行数字 ...
- static块的本质
在网上看到了下面的一段代码: public class Test { static { _i = 20; } public static int _i = 10; public static void ...
- 网页调用vlc并播放网络视频
环境:windows/android/ios windows端保存以下内容为reg文件并运行 Windows Registry Editor Version 5.00 [HKEY_CLASSES_RO ...
- 福大软工1816:Beta总结
第三视角Beta答辩总结 博客链接以及团队信息 组长博客链接 成员信息(按拼音排序) 姓名 学号 备注 张扬 031602345 组长 陈加伟 031602204 郭俊彦 031602213 洪泽波 ...