题目链接: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. cookie 跨域解决方法

    1.Nginx 正向和反向代理的区别 正向代理和反向代理的区别:正向代理隐藏真实客户端,反向代理隐藏真实服务端,图示: 2.cookie跨域问题 因为cookie存在跨域问题,其中一个解决方法是,设置 ...

  2. python 验证码识别示例(二) 复杂验证码识别

     在这篇博文中手把手教你如何去分割验证,然后进行识别. 一:下载验证码 验证码分析,图片上有折线,验证码有数字,有英文字母大小写,分类的时候需要更多的样本,验证码的字母是彩色的,图片上有雪花等噪点,因 ...

  3. Asp.Net WebApi上传图片

    webapi using System; using System.Collections; using System.Collections.Generic; using System.Diagno ...

  4. TF的模型文件

    TF的模型文件 标签(空格分隔): TensorFlow Saver tensorflow模型保存函数为: tf.train.Saver() 当然,除了上面最简单的保存方式,也可以指定保存的步数,多长 ...

  5. spring相关面试题

    1.spring有依赖的bean,怎么加载? 2.spring怎么解决循环依赖? https://blog.csdn.net/u010853261/article/details/77940767 h ...

  6. find ctime 加减n时间范围

    看下atime的时间解释:-atime n File was last accessed n*24 hours ago. When find figures out how many 24-hour ...

  7. selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: None;Message: unexpected alert open: {Alert text : 您点击的频率过快!请稍后再试}

    报错 Traceback (most recent call last): File "C:/myFiles/code/cnki/cnki_1/core/knavi.py", li ...

  8. Netty WebSocket 开发

    代码: Server package netty.protocol.websocket.server; import io.netty.bootstrap.ServerBootstrap; impor ...

  9. vscode c++ 编译生成后,调试时无法命中断点

    //test.cpp #include <stdio.h> ; void print_line(char *str) { if (str != NULL) printf("%s\ ...

  10. 使用glusterfs 作为 kubernetes PersistentVolume PersistentVolumeClaim 持久化仓库,高可用Rabbitmq,高可用mysql,高可用redis

    glusterfs 怎么集群,网上一搜铺天盖地的 可利用这个特点做单节点高可用,因为K8S 哪怕节点宕机了 master 会在随意一台节点把挂掉的复活 当然我是在自己的环境下跑,经过网络的gluste ...