(简单) POJ 3984 迷宫问题,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,
0, 0, 0, 1, 0,
};
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
#include<iostream>
#include<cstring>
#include<queue>
#include<utility> using namespace std; typedef struct pair<int,int> pii; int vis[][];
int fa[][]; bool judge(int x,int y)
{
if(x<||y<||x>||y>)
return ; if(vis[x][y])
return ; return ;
} void slove()
{
queue < pii > que;
pii temp,temp1;
int t1,t2; que.push(make_pair(,));
vis[][]=; while(!que.empty())
{
temp=que.front();
que.pop(); t1=temp.first/;
t2=temp.first%;
fa[t1][t2]=temp.second; if(t1==&&t2==)
return; --t1;
if(judge(t1,t2))
{
vis[t1][t2]=;
temp1=make_pair(t1*+t2,temp.first);
que.push(temp1);
}
t1+=;
if(judge(t1,t2))
{
vis[t1][t2]=;
que.push(make_pair(t1*+t2,temp.first));
}
--t1;
--t2;
if(judge(t1,t2))
{
vis[t1][t2]=;
que.push(make_pair(t1*+t2,temp.first));
}
t2+=;
if(judge(t1,t2))
{
vis[t1][t2]=;
que.push(make_pair(t1*+t2,temp.first));
}
}
} void showans()
{
int cou=;
int ans[];
int temp=; while(temp)
{
ans[cou++]=temp;
temp=fa[temp/][temp%];
} cout<<"(0, 0)"<<endl;
for(int i=cou-;i>=;--i)
cout<<'('<<ans[i]/<<", "<<ans[i]%<<')'<<endl;
} int main()
{
ios::sync_with_stdio(false); while(cin>>vis[][])
{
for(int j=;j<;++j)
cin>>vis[][j]; for(int i=;i<;++i)
for(int j=;j<;++j)
cin>>vis[i][j]; slove();
showans();
} return ;
}
(简单) POJ 3984 迷宫问题,BFS。的更多相关文章
- POJ 3984 - 迷宫问题 - [BFS水题]
题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...
- POJ 3984 迷宫问题 bfs 难度:0
http://poj.org/problem?id=3984 典型的迷宫问题,记录最快到达某个点的是哪个点即可 #include <cstdio> #include <cstring ...
- [POJ 3984] 迷宫问题(BFS最短路径的记录和打印问题)
题目链接:http://poj.org/problem?id=3984 宽度优先搜索最短路径的记录和打印问题 #include<iostream> #include<queue> ...
- 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, ...
- poj 3984 迷宫问题 bfs
学会这道水题之后我懂得了不少哈,首先水题也能学到不少知识,尤其像我这样刚入门的小菜鸟,能学到一些小技巧. 然后就是可以从别人的代码里学到不一样的思路和想法. 这题就是求最短的路径,首先想到就是用bfs ...
- POJ - 3984 迷宫问题 bfs解法
#include<stdio.h> #include<string.h> #include<algorithm> #include<stack> usi ...
- POJ 3984 迷宫问题 (BFS + Stack)
链接 : Here! 思路 : BFS一下, 然后记录下每个孩子的父亲用于找到一条路径, 因为寻找这条路径只能从后向前找, 这符合栈的特点, 因此在输出路径的时候先把目标节点压入栈中, 然后不断的向前 ...
- BFS(最短路+路径打印) POJ 3984 迷宫问题
题目传送门 /* BFS:额,这题的数据范围太小了.但是重点是最短路的求法和输出路径的写法. dir数组记录是当前点的上一个点是从哪个方向过来的,搜索+,那么回溯- */ /************* ...
- POJ 3984 迷宫问题(简单bfs+路径打印)
传送门: http://poj.org/problem?id=3984 迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
随机推荐
- 我为什么坚持DBA一定要懂开发
我为什么坚持DBA一定要懂开发时间 2016-03-23 15:34:08 张碧池的幸福生活原文 http://pottievil.com/我为什么坚持dba一定要懂开发/主题 DBA 数据库最近手头 ...
- Parade
Parade time limit per test 1 second memory limit per test 256 megabytes input standard input output ...
- POJ3264/RMQ
题目链接 /* 询问一段区间内的元素差值最大是多少,用RMQ维护一个最大值和一个最小值,相减即可. */ #include<cstdio> #include<cstring> ...
- 几个SQL语句笔试题
1.表A和表B具有完全相同的结构,查出表A中有但表B中没有的数据: create table A( id int , name ), password ) ); create table B( id ...
- Android内核驱动程序的编写和编译过程
注意:涉及的代码为android内核代码而不是android源码. 在智能手机时代,每个品牌的手机都有自己的个性特点.正是依靠这种与众不同的个性来吸引用户,营造品牌凝聚力和用户忠城度,典型的代表非ip ...
- Mac机上安装虚拟机
1.首先要下载虚拟机VMware.网址http://pan.baidu.com/share/init?shareid=1519279671&uk=1434905687,连接该网址,下载里边的这 ...
- Codeforces 691A Fashion in Berland
水题. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #includ ...
- Adobe Acrobat 9 Pro 注册码
来自百度知道,记录与此,以备后用http://zhidao.baidu.com/question/177914535.html 如果你的系统盘是C盘,那么就删除:c:/Documents and Se ...
- MyEclipse8.5优化经验
第一步: 取消自动validation validation有一堆,什么xml.jsp.jsf.js等等,我们没有必要全部都去自动校验一下,只是需要的时候才会手工校验一下! 取消方法: ...
- foo bar的意思
有些朋友问:foo, bar是什么意思, 为什么C++书籍中老见到这个词. 我google了一下, 发现没有很好的中文答案.这个问题,在维基百科上有很好的回答.在这里翻译给大家. 译文: 术语foob ...