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表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
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)
题解:
本题首先想到BFS一遍,数组dis存最短路径(此方法来自《挑战程序设计竞赛(第2版)》-宽度优先搜索,迷宫最短路径-P34)。但没办法记录走过的路径,那该怎么办呢?
我想到的是开一个pair数组sha,记录此点的上一个点的x和y。之后开循环,根据本x,y继续找上个x,y。最后判断sha是否x==0,y==0,如果满足,那么就退出循环。
我把答案存在了sta栈里(其实也可以存在数组里,最后倒输出即可。但作为一个合格的IT男,能装逼则装逼,呵呵:-D) 最后输出sta.top() 之后sta.pop() 最后答案即可求出。
AC代码:
#include <iostream>
#include <cmath>
#include <stdio.h>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <iomanip>
#include <algorithm>
using namespace std;
#define cle(a,b) memset(a,b,sizeof(a))
#define rep(i,b) for(unsigned i=0;i<(b);i++)
const int INF=9999; int a[5][5],dis[5][5],dx[]= {0,1,-1,0},dy[]= {1,0,0,-1};
pair<int ,int> sha[5][5]; struct node
{
int x,y;
};
void bfs()
{
queue<node> que;
node pre,next;
pre.x=0;
pre.y=0;
dis[0][0]=0;
que.push(pre);
while(que.size())
{
pre=que.front();
que.pop();
next=pre;
for (int i=0; i<4; i++)
{
int nx=next.x+dx[i],ny=next.y+dy[i];
if (nx<5&&nx>=0&&ny<5&&ny>=0&&dis[nx][ny]==INF&&a[nx][ny]!=1)
{
dis[nx][ny]=dis[next.x][next.y]+1;
node tem;
tem.x=nx;
tem.y=ny;
sha[nx][ny].first=next.x;
sha[nx][ny].second=next.y;
que.push(tem);
}
}
}
} int main()
{
rep(i,5)
rep(j,5)
{
dis[i][j]=INF;
sha[i][j].first=INF,sha[i][j].second=INF;
scanf("%d",&a[i][j]);
}
bfs();
int xx=sha[4][4].first,yy=sha[4][4].second;
stack< node > sta;
while(xx!=0||yy!=0)
{
node te;
te.x=xx;
te.y=yy;
sta.push(te);
int nxx=xx,nyy=yy;
xx=sha[nxx][nyy].first;
yy=sha[nxx][nyy].second;
}
puts("(0, 0)");
while(sta.size())
{
node pa;
pa=sta.top();
printf("(%d, %d)\n",pa.x,pa.y);
sta.pop();
}
puts("(4, 4)");
return 0;
}
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 ...
- POJ 3984 迷宫问题
K - 迷宫问题 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Sta ...
随机推荐
- 第九章 C语言在嵌入式中的应用
上章回顾 编码的规范和程序版式 版权管理和申明 头文件结构和作用 程序命名 程序注释和代码布局规范 assert断言函数的应用 与0或NULL值的比较 内存的分配和释放细节,避免内存泄露 常量特性 g ...
- Linux系统编程@多线程编程(二)
线程的操作 线程标识 线程的ID表示数据类型:pthread_t (内核中的实现是unsigned long/unsigned int/指向pthread结构的指针(不可移植)几种类型) 1.对两个线 ...
- Unity代码热更新方案 JSBinding + SharpKit 首页
目前Unity的代码更新方案有很多,主要以lua为主. JSBinding + SharpKit 是一种新的技术,他做了两件事情: JSBinding将C#导出到 JavaScript (引擎是 Mo ...
- X-UA-Compatible设置兼容模式
原文地址:http://www.cnblogs.com/kingboy2008/archive/2011/07/01/2226424.html IE浏览器的兼容性一直是网站开发人员头疼的事情,众所 ...
- 【转】用 PHP 内置函数 file_put_contents 写入文件
PHP 内置函数 file_put_contents 用于写入文件. file_put_contents 函数最简单的写法,可以只用两个参数,一个是文件路径,一个是要写入的内容,语法如下: file_ ...
- 09_android入门_采用android-async-http开源项目的GET方式或POST方式实现登陆案例
根据08_android入门_android-async-http开源项目介绍及使用方法的介绍,我们通过最常见的登陆案例进行介绍android-async-http开源项目中有关类的使用.希望对你学习 ...
- C#.NET vs2010中使用IrisSkin4.dll轻松实现WinForm窗体换肤功能
IrisSkin2.dll是一款很不错的免费皮肤控件,利用它可以轻松的实现WinForm窗体换肤 然而IrisSkin2.dll只能在.NET Faremwork 4.0以及之前的版本使用,所以要在V ...
- Keepalived+MySQL实现高可用(转)
http://www.cnblogs.com/wingsless/p/4033093.html MHA高可用 http://www.cnblogs.com/gomysql/p/3856484.ht ...
- jQuery validate基本原则
Markup recommendations Each input has a label associated with it: The for-attribute of the label ref ...
- 多线程操作(ThreadPool.QueueUserWorkItem
主线程: private void GetPolicy_Load(object sender, EventArgs e) { ////ThreadPool.QueueUserWorkItem(new ...