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 ...
随机推荐
- 解锁Spring框架姿势1
Spring 介绍:Spring 框架是一个Java平台,它为开发Java应用程序提供全面的基础架构支持.Spring负责基础架构,因此您可以专注于应用程序的开发. Spring可以让您从" ...
- java温故而知新(9)OOP(面向对象编程)理念
Object Oriented Programming (面向对象的程序设计) 1.定义 面向对象程序设计(OOP)的具体定义很难下,也很容易因此而引起争论,在 Object-Orie ...
- 合理使用线程池 ThreadPool.QueueUserWorkItem()
//==>自建线程 new Thread(() => { //线程任务 Console.WriteLine(Thread.CurrentThread.ManagedThreadId); } ...
- 扩展运算符&reset运算符
扩展运算符用三个点号表示,功能是把数组或类数组对象展开成一系列用逗号隔开的值 var foo = function(a, b, c) { console.log(a); console.log(b); ...
- 09_Redis持久化——AOF方式
[AOF简述] AOF(Append-only) Redis每次接受到一条改变数据的命令时,它会把该命令写到一个AOF文件中(只记录写操作,不记录读操作),当Redis启动时,它通过执行AOF文件中的 ...
- shell 脚本解压war包+备份+tomcat自动关闭+启动
公司的开发环境每次替换war包时候,老是需要重新上传并且手动解压,然后再去重启tomcat.觉得这样子太麻烦了,于是写了一个shell脚本,自动解压+备份+tomcat自动关闭+启动.代码如下: #关 ...
- canvas中的非零环绕原则
非零环绕原则: 如果需要判断某一个区域是否需要填充颜色. 就从该区域中随机的选取一个点. 从这个点拉一条直线出来, 一定要拉到图形的外面. 此时以该点为圆心.看穿过拉出的直线的线段. 如果是顺时针方向 ...
- Linux下mysql安装过程
到mysql官网下载mysql编译好的二进制安装包,在下载页面Select Platform:选项选择linux-generic,然后把页面拉到底部,64位系统下载Linux - Generic (g ...
- ssh_exchange_identification: Connection closed by remote host
用服务器内网ip连接同子网服务器的时候显示ssh_exchange_identification: Connection closed by remote host 防火墙什么都关闭了,还是显示这个问 ...
- Python学习---重点模块之subprocess
subprocess是用来执行系统程序,查看系统的模块, 查看当前目录 第一种方法: import subprocess # subprocess会单独自己开辟一个线程,内部是多线程 # stdout ...