题目链接:http://poj.org/problem?id=3984

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

一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。

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水题。

AC代码:

#include<cstdio>
#include<cstring>
#include<queue>
#include<stack>
using namespace std;
const int dx[]={,,,-};
const int dy[]={,,-,}; int mp[][];
struct Node{
int x,y;
int cnt;
Node(){}
Node(int _x,int _y,int _cnt) {
x=_x, y=_y, cnt=_cnt;
}
inline bool out() {
return x< || x>= || y< || y>=;
}
};
queue<Node> Q;
bool vis[][];
Node pre[][];
stack<Node> ans; int main()
{
memset(mp,,sizeof(mp));
for(int i=;i<;i++) for(int j=;j<;j++) scanf("%d",&mp[i][j]); memset(vis,,sizeof(vis));
Q.push((Node){,,});
vis[][]=;
while(!Q.empty())
{
Node now=Q.front(); Q.pop();
if(now.x== && now.y==)
{
ans.push(now);
break;
}
for(int k=;k<;k++)
{
Node nxt=Node(now.x+dx[k],now.y+dy[k],now.cnt+);
if(nxt.out()) continue;
if(mp[nxt.x][nxt.y]) continue;
if(vis[nxt.x][nxt.y]) continue;
Q.push(nxt);
vis[nxt.x][nxt.y]=;
pre[nxt.x][nxt.y]=now;
}
} while(ans.top().cnt) ans.push(pre[ans.top().x][ans.top().y]);
while(!ans.empty())
{
printf("(%d, %d)\n",ans.top().x,ans.top().y);
ans.pop();
}
}

POJ 3984 - 迷宫问题 - [BFS水题]的更多相关文章

  1. poj 3984 迷宫问题 bfs

    学会这道水题之后我懂得了不少哈,首先水题也能学到不少知识,尤其像我这样刚入门的小菜鸟,能学到一些小技巧. 然后就是可以从别人的代码里学到不一样的思路和想法. 这题就是求最短的路径,首先想到就是用bfs ...

  2. 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, ...

  3. POJ 3984 迷宫问题 bfs 难度:0

    http://poj.org/problem?id=3984 典型的迷宫问题,记录最快到达某个点的是哪个点即可 #include <cstdio> #include <cstring ...

  4. [POJ 3984] 迷宫问题(BFS最短路径的记录和打印问题)

    题目链接:http://poj.org/problem?id=3984 宽度优先搜索最短路径的记录和打印问题 #include<iostream> #include<queue> ...

  5. POJ 3126 Prime Path bfs, 水题 难度:0

    题目 http://poj.org/problem?id=3126 题意 多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为 ...

  6. POJ - 3984 迷宫问题 bfs解法

    #include<stdio.h> #include<string.h> #include<algorithm> #include<stack> usi ...

  7. POJ 3984 迷宫问题 (BFS + Stack)

    链接 : Here! 思路 : BFS一下, 然后记录下每个孩子的父亲用于找到一条路径, 因为寻找这条路径只能从后向前找, 这符合栈的特点, 因此在输出路径的时候先把目标节点压入栈中, 然后不断的向前 ...

  8. BFS(最短路+路径打印) POJ 3984 迷宫问题

    题目传送门 /* BFS:额,这题的数据范围太小了.但是重点是最短路的求法和输出路径的写法. dir数组记录是当前点的上一个点是从哪个方向过来的,搜索+,那么回溯- */ /************* ...

  9. poj 3080 Blue Jeans(水题 暴搜)

    题目:http://poj.org/problem?id=3080 水题,暴搜 #include <iostream> #include<cstdio> #include< ...

随机推荐

  1. java对对象或者map的属性进行排序

    package com.xkj.spider.mpb.util; import java.lang.reflect.Method; import java.util.HashMap; import j ...

  2. csproj文件中copy指令的使用方式

    实际开发中有很多项目需要引用第三方的dll或者资源文件,且文件比较多,在运行时这些文件需要被拷贝到BIN目录. 使用VS自带的"复制到输出目录",似然方便,但是比较不零活,经过多次 ...

  3. svn安装教程

    svn服务器端下载(VisualSVN) 安装包,选择windows版的VisualSVN-Server https://www.visualsvn.com/downloads/ svn客户端下载(T ...

  4. [mvc] 过滤器filter一览

    1.mvc的过滤器种类

  5. Failed to execute 'write' on 'Document'动态载入的js不能执行write

    统计代码一般都是直接一个标签,插入js,标签放在哪里,统计图表就放在哪里! 我现在是稍微改了一下,我自己加了一点js,在页面所有元素都加载完成之后我再动态的把统计js插入到我需要的地方. 统计代码的s ...

  6. VirtualBox通过Host-Only网络连接方式实现宿主机与虚拟机通信

    适用情况 (1)没有联网, 不插网线 (2)宿主机直接连接宽带(无路由器) 情景: 宿主机 Windows 7 虚拟机 Windows XP 虚拟机安装了SQLServer2005,宿主机想连接使用虚 ...

  7. layui 笔记

    弹出层 点击事件 <!DOCTYPE html> <html> <head> <title></title> {load href=&quo ...

  8. 解决ubuntu开机进入grub界面的问题

    开机显示GRUB界面显示如下字样,几秒后自动进入登录界面 *Ubuntu Advanced options for Ubuntu .... 解决方案: 1.编辑grub文件 sudo vim /etc ...

  9. 【Zookeeper系列】ZooKeeper伸缩性(转)

    原文地址:https://www.cnblogs.com/sunddenly/p/4143306.html 一.ZooKeeper中Observer 1.1 ZooKeeper角色 经过前面的介绍,我 ...

  10. nginx的日志配置

    本文转自:https://www.cnblogs.com/biglittleant/p/8979856.html 版权归属原作者!!!!!! nginx access日志配置 access_log日志 ...