迷宫问题(bfs+记录路径)
题目链接:
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105278#problem/K
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) 题解:注意暴力搜索记录路径的题,一个学到的很好的方法就是开一个数组记录当前节点的父亲节点,然后最后输出的时候,从最后一个点依次向前面找,按照父亲找父亲的方法找下去,最后就是路径了
#include<cstdio>
#include<cstring>
#include<stack>
#include<queue>
#include<string>
using namespace std;
int mp[][];
struct Node{
int x;
int y;
};
Node fa[][];
bool vis[][];
bool ck(Node k)
{
if(k.x<=&&k.x>=&&k.y>=&&k.y<=) return true;
return false;
} int go[][] = {{,},{-,},{,},{,-}}; void bfs(){
queue<Node>q;
memset(vis,,sizeof(vis));
Node tm;
tm.x = ;
tm.y = ;
q.push(tm);
vis[][] = ;
while(!q.empty()){
Node tm1 = q.front();q.pop();
Node fl;
for(int i = ; i < ; i++){
fl.x = tm1.x+go[i][];
fl.y = tm1.y+go[i][];
if(ck(fl)&&vis[fl.x][fl.y]==&&mp[fl.x][fl.y]==){
q.push(fl);
vis[fl.x][fl.y] = ;
fa[fl.x][fl.y] = tm1;
}
}
}
} int main()
{
for(int i = ; i < ; i++)
for(int j = ; j < ; j++)
scanf("%d",&mp[i][j]);
bfs();
stack<Node>v;
Node ff = fa[][];
while(!(ff.x==&&ff.y==)){
v.push(ff);
ff = fa[ff.x][ff.y];
}
printf("(0, 0)\n");
while(!v.empty()){
printf("(%d, %d)\n",v.top().x,v.top().y);
v.pop();
}
printf("(4, 4)\n");
return ;
}
迷宫问题(bfs+记录路径)的更多相关文章
- POJ.3894 迷宫问题 (BFS+记录路径)
POJ.3894 迷宫问题 (BFS+记录路径) 题意分析 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, ...
- 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, ...
- 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 3414 Pots,BFS+记录路径。
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- poj 3414 Pots 【BFS+记录路径 】
//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...
- sdut oj 3058 路线冲突问题(BFS+记录路径算法,回溯路径 )
路线冲突问题 题目描述 给出一张地图,地图上有n个点,任意两点之间有且仅有一条路.点的编号从1到n. 现在兵团A要从s1到e1,兵团B要从s2到e2,问两条路线是否会有交点,若有则输出交点个数,否出输 ...
随机推荐
- CSS篇(上)
紧接着HTML篇的CSS篇开啦,老铁们快来围观... 1.介绍一下标准的CSS盒子模型?低版本IE的盒子模型有什么不同? 1>有两种:IE盒子模型 W3C盒子模型 2>盒模型:内 ...
- 如何严格设置php中session过期时间
如何严格限制session在30分钟后过期! 1.设置客户端cookie的lifetime为30分钟: 2.设置session的最大存活周期也为30分钟: 3.为每个session值加入时间戳,然后在 ...
- sudo 做不到的事
本文是经验帖,以后遇到类似的情况会持续更新到这篇文章 普通用户使用sudo会遇到以下情况 1.字符流无法写入到 /var/log/messages /var/log/secure (实际上这些文件一旦 ...
- 佛祖保佑永无bug的源代码
${AnsiColor.BRIGHT_YELLOW} ${AnsiColor.BRIGHT_RED}_ooOoo_${AnsiColor.BRIGHT_YELLOW} ${AnsiColor.BRIG ...
- 安装spark单机环境
(假定已经装好的hadoop,不管你装没装好,反正我是装好了) 1 下载spark安装包 http://spark.apache.org/downloads.html 下载spark-1.6.1-bi ...
- MySQL数据库学习: 01 —— 数据库的概述
壹 概述 一 了解SQL 1.1 数据库基础 1.1.1 什么是数据库 数据库(database)保存有组织的数据的容器(通常是一个文件或一组文件). 易混淆:人们常常用“数据库”这个词语来代表他们使 ...
- vs2017中生成.Net Standard Libarary的Nuget Package
场景: Project A 对Project B存在 project to project reference.这种场景下必须为两者都生成nuget package.这样在load Project A ...
- python模块安装报错 :error: command 'gcc' failed with exit status 1
参考:http://blog.csdn.net/fenglifeng1987/article/details/38057193 解决方法 yum install gcc libffi-devel py ...
- 为什么很多第三方接口,都改成了基于http,直接传递json数据的方式来代替webservice?
这实际上是三个问题,从WebService到今天流行的RESTful API(JSON) over HTTP,经历了数次变革 1 WebService有很多协议,为什么HTTP比较流行? WebSer ...
- css动画-音频跳动
css动画-音频跳动 ---------------------------------------------------------------------------- ------------ ...