(简单) 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 ...
随机推荐
- iOS开发frame, contentSize, contentOffset, contentInset 区别联系浅析
1. 概述 iOS开发中,必然会涉及到布局相关问题,frame,bounds,contenSize,contentOffset,contentInset这几个布局相关概念让许多初学者感到困惑.虽然初步 ...
- HDU1896Stones(优先队列)
Stones Time Limit : 5000/3000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total Submis ...
- HDU 5833 (2016大学生网络预选赛) Zhu and 772002(高斯消元求齐次方程的秩)
网络预选赛的题目……比赛的时候没有做上,确实是没啥思路,只知道肯定是整数分解,然后乘起来素数的幂肯定是偶数,然后就不知道该怎么办了… 最后题目要求输出方案数,首先根据题目应该能写出如下齐次方程(从别人 ...
- AS3条件编译
package { import flash.display.Sprite; public class Main extends Sprite { public function Main() { s ...
- JDBC的超时原理
1.什么是JDBC jdbc是业务系统连接数据的标准API.SUN公司一共定义了4中类型的JDBC:JDBC-ODBC桥:Native-API 驱动:Network-Protocol 驱动:Datab ...
- PAT1006
At the beginning of every day, the first person who signs in the computer room will unlock the door, ...
- java OPENCV 连通域, Imgproc.findContours 例子,参数说明
http://stackoverflow.com/questions/29491669/real-time-paper-sheet-detection-using-opencv-in-android/ ...
- HDU 3487 Play with Chain | Splay
Play with Chain Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Javascript面向对象编程(二):构造函数的继承
这个系列的第一部分,主要介绍了如何"封装"数据和方法,以及如何从原型对象生成实例. 今天要介绍的是,对象之间的"继承"的五种方法. 比如,现在有一个" ...
- virtualbox, vt-s, rmmod kvm-intel
http://blog.sina.com.cn/s/blog_4e7b97f00100fltu.html装完后,去下个jdk,坑爹的Oracle非要让老子注册,试了几次都没有成功,密码不合格拉,邮箱已 ...