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 ...
随机推荐
- jasperreports+iReport+jatoolsPrinter制作报表笔记
此文章是基于 EasyUI+Knockout实现经典表单的查看.编辑 一. 准备工作 1. 点击此下载相关的文件,并把 ims 文件夹放到 ims 工程对应的路劲下 2. 参考网址:杰创打印控件 二. ...
- javascript多浏览器的兼容
一.document.formName.item(”itemName”) 问题 问题说明:IE下,可以使用 document.formName.item(”itemName”) 或 document. ...
- MySQL数据库(1)----入门级操作
1.在服务器主机上以 root 用户登陆,创建位于其他客户端的新用户: mysql> CREATE USER 'newuser'@'192.168.1.109' IDENTIFIED BY 'p ...
- idea打jar包
昨天碰到个问题:使用idea打成jar包,但是在测试环境一直报错.参考: http://blog.csdn.net/aotian16/article/details/52198378 之后发现原来的j ...
- DELPHI 小结
//十六进制(S)-->>十进制(I) [重写:Jey]function hextoint(s: string): Integer; begin //$代表16进制 ...
- [poj 2479] Maximum sum -- 转载
转自 CSND 想看更多的解题报告: http://blog.csdn.net/wangjian8006/article/details/7870410 ...
- incast.tcl
# Basic Incast Simulation # Check Args if {$argc != 5} { puts "Usage: ns incast <srv_num> ...
- Windows(7)上不能启动MySQL服务(位于本地计算机上)错误1067 :进程意外终止
就这段时间,很多人在抱怨为什么自己的MySQL又打不开问题. 就“Windows(7)上不能启动MySQL服务(位于本地计算机上)错误1067 :进程意外终止”这个问题,我想到了几种方案解决: 一.首 ...
- Linux->Ubuntu下配置telnet环境
1.首先查看telnet运行状态 netstat -a | grep telnet 输出为空,表示没有开启该服务 2.安装openbsd-inetd apt-get install openbsd-i ...
- $.on()方法和addEventListener改变this指向
jQuery $.on()方法和addEventListener改变this指向 标签(空格分隔): jQuery JavaScript jQuery $.on() jq的绑定事件使用$([selec ...