(简单) 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 ...
随机推荐
- 运用Merge Into实现增加或更新数据
declare @SqlStr as nvarchar(max) set @SqlStr=N'Merge Into [Categories] t USING(VALUES (9,''rice'','' ...
- [分享]Host文件的原理解释及应用说明
Host文件的原理解释及应用说明 Host文件位置及打开方式: Window系统中有个Hosts文件(没有后缀名), Windows 98系统下该文件在Windows目录,在Windows 2 ...
- Simple But Useful Samples About 'grep' Command(简单实用的grep 命令)
Do the following: grep -rnw '/path/to/somewhere/' -e "pattern" -r or -R is recursive, -n i ...
- CSS中如何把Span标签设置为固定宽度
一.形如<span>ABC</span>独立行设置SPAN为固定宽度方法如下: span {width:60px; text-align:center; display:blo ...
- Android OpenGL ES(十一)绘制一个20面体 .
前面介绍了OpenGL ES所有能够绘制的基本图形,点,线段和三角形.其它所有复杂的2D或3D图形都是由这些基本图形构成. 本例介绍如何使用三角形构造一个正20面体.一个正20面体,有12个顶点,20 ...
- CentOS下载及版本选择-CentOS LiveCD、LiveDVD和BinDVD区别
1.CentOS系统镜像有两个,安装系统只用到第一个镜像即CentOS-6.x-i386-bin-DVD1.iso(32位)或者CentOS-6.x-x86_64-bin-DVD1.iso(64位), ...
- ABI & API
API defines the programning language and function entry point, arguments type, order. ABI defines th ...
- Windows API 之 CreateToolhelp32Snapshot
CreateToolhelp32Snapshot: 参考: https://msdn.microsoft.com/en-us/library/ms682489%28VS.85%29.aspx HAND ...
- opencart配置税率
1.System->Localisation->Geo Zones新增税收区域 2.System->Localisation->Taxes->Tax Rates新增税率 ...
- 判断iPhone/android手机
JS判断请求来自Android手机还是iPhone手机,根据不同的手机跳转到不同的链接. var browser = {versions: function () {var u = navigator ...