POJ 3984 迷宫问题(简单bfs+路径打印)
传送门:
http://poj.org/problem?id=3984
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 33105 | Accepted: 18884 |
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)
Source
分析:
虽然bfs写得多一点,但路径打印的这还是第1个!!!
利用栈的特性打印出来就好
code:
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <string.h>
#include<queue>
#include<stack>
using namespace std;
int G[][];
int dir[][]={,,,,-,,,-};
int vis[][];
struct node
{
int x,y;
}pre[][]; void pri()
{
stack<node> s;
node p;
p.x=,p.y=; while()
{
s.push(p);
if(p.x==&&p.y==)
break;
p=pre[p.x][p.y];
}
int x,y;
while(!s.empty())
{
x=s.top().x;
y=s.top().y;
printf("(%d, %d)\n",x,y);
s.pop();
}
}
void bfs(int x,int y)
{
queue<node> q;
node p,next; p.x=x,p.y=y;
q.push(p);
vis[x][y]=; while(!q.empty())
{
p=q.front();
q.pop(); if(p.x==&&p.y==)
{
pri();
return ;
}
for(int i=;i<;i++)
{
next.x=p.x+dir[i][];
next.y=p.y+dir[i][]; if(next.x>=&&next.x<&&next.y>=&&next.y<&&vis[next.x][next.y]==&&G[next.x][next.y]==)
{
pre[next.x][next.y]=p;
vis[next.x][next.y]=;
q.push(next);
}
}
}
}
int main()
{
memset(G,,sizeof(G));
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
cin>>G[i][j];
}
}
memset(vis,,sizeof(vis));
memset(pre,,sizeof(pre));
bfs(,);
return ;
}
POJ 3984 迷宫问题(简单bfs+路径打印)的更多相关文章
- poj 3984 迷宫问题【bfs+路径记录】
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10103 Accepted: 6005 Description ...
- POJ 3984 迷宫问题【BFS/路径记录/手写队列】
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31428 Accepted: 18000 Description 定义 ...
- (简单) 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, ...
- 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, ...
- POJ - 3984 迷宫问题 【BFS】
题目链接 http://poj.org/problem?id=3984 思路 因为要找最短路 用BFS 而且 每一次 往下一层搜 要记录当前状态 之前走的步的坐标 最后 找到最短路后 输出坐标就可以了 ...
- POJ——3984迷宫问题(BFS+回溯)
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14568 Accepted: 8711 Description ...
- BFS(最短路+路径打印) POJ 3984 迷宫问题
题目传送门 /* BFS:额,这题的数据范围太小了.但是重点是最短路的求法和输出路径的写法. dir数组记录是当前点的上一个点是从哪个方向过来的,搜索+,那么回溯- */ /************* ...
- POJ.3894 迷宫问题 (BFS+记录路径)
POJ.3894 迷宫问题 (BFS+记录路径) 题意分析 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, ...
- POJ 3984 迷宫问题
K - 迷宫问题 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Sta ...
随机推荐
- Windows下etc文件夹
etc etcetera[ɛtsɛtərə]缩写 等等的意思 放置一些其他文件
- cookie结合js 实现记住的拖拽
哈喽!!!我胡汉三又回来啦!!!有木有记挂挪啊!咱们今天说一个 cookie结合JS的小案例哦! 话不多说直接上代码: <!DOCTYPE html> <html> <h ...
- ArcGIS DataStore手册——管理篇
第二章:ArcGIS DataStore管理维护 1.备份管理 备份的目的在于发生原始数据损坏或其他突发情况时,可避免数据丢失,并可快速的使用备份数据来恢复,以保证服务仍可使用. 单机模式下,可使用D ...
- linux crontab 的使用
linux crontab 的使用 准备(实验楼需要,实际环境不需要):sudo service rsyslog startsudo cron -f & crontab 使用添加任务:cron ...
- LeetCode--Combination Sum --ZZ
http://blog.csdn.net/linhuanmars/article/details/20828631 这个题是一个NP问题,方法仍然是N-Queens中介绍的套路.基本思路是先排好序,然 ...
- tomcat运行报错Failed to start component [StandardEngine[Catalina].StandardHost[localhost].
tomcat运行报错Failed to start component [StandardEngine[Catalina].StandardHost[localhost].多半情况是找不到jar包 解 ...
- 爬虫入门之Scrapy 框架基础功能(九)
Scrapy是用纯Python实现一个为了爬取网站数据.提取结构性数据而编写的应用框架,用途非常广泛. 框架的力量,用户只需要定制开发几个模块就可以轻松的实现一个爬虫,用来抓取网页内容以及各种图片,非 ...
- layui 设计资源——2.0 版本的 Axure 组件包,产品交互设计利器
大家好,很久不见,这次为大家分享的是 layui_2.0版本的axure组件包,在去年发布的 layui Axure 1.0 中(见:http://fly.layui.com/jie/9842/ )赢 ...
- Topshelf Configuration z
Topshelf Configuration While the Quickstart gives you enough to get going, there are many more featu ...
- CVE-2014-0322漏洞成因与利用分析
CVE-2014-0322漏洞成因与利用分析 1. 简介 此漏洞是UAF(Use After Free)类漏洞,即引用了已经释放的内存,对指定内存处的值进行了加1.其特点在于攻击者结合flash实现了 ...