**链接 : ** Here!

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

**注意 : ** 不要把局部变量压入栈中, 这样就直接段错误了◔ ‸◔


/*************************************************************************
> File Name: 3984-迷宫问题.cpp
> Author:
> Mail:
> Created Time: 2017年11月29日 星期三 19时28分22秒
************************************************************************/ #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std; #define MAX_N 10
int G[MAX_N][MAX_N];
int vis[MAX_N][MAX_N] = {0};
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
struct Point {
Point() {}
Point(int x, int y, Point *father) : x(x), y(y), father(father) {}
int x, y;
Point *father;
}; void read() {
for (int i = 0 ; i < 5 ; ++i) {
for (int j = 0 ; j < 5 ; ++j) {
scanf("%d", &G[i][j]);
}
}
}
bool check(Point pt) {
if (pt.x < 0 || pt.x >= 5 || pt.y < 0 || pt.y >= 5 || vis[pt.x][pt.y] || (G[pt.x][pt.y] == 1)) return false;
return true;
}
void solve() { Point st(0, 0, NULL), last_pt;
Point pt[MAX_N * MAX_N];
int ind = 0; queue<Point> que;
que.push(st);
vis[st.x][st.y] = 1; while (!que.empty()) {
pt[ind] = que.front();
que.pop();
Point *now = &pt[ind];
last_pt = pt[ind];
++ind;
for (int i = 0 ; i < 4 ; ++i) {
int tx = now->x + dx[i];
int ty = now->y + dy[i];
if (!check(Point(tx, ty, NULL))) continue;
pt[ind].x = tx;
pt[ind].y = ty;
pt[ind].father = now;
vis[pt[ind].x][pt[ind].y] = 1;
que.push(pt[ind]);
++ind;
}
}
stack<Point *> myStack;
Point *p = &last_pt;
while (p->father != NULL) {
myStack.push(p);
p = p->father;
}
printf("(0, 0)\n");
while (!myStack.empty()) {
p = myStack.top();
myStack.pop();
printf("(%d, %d)\n", p->x, p->y);
}
}
int main() {
read();
solve();
return 0;
}

POJ 3984 迷宫问题 (BFS + Stack)的更多相关文章

  1. POJ 3984 - 迷宫问题 - [BFS水题]

    题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...

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

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

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

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

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

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

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

  6. poj 3984 迷宫问题 bfs

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

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

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

  8. POJ 3984 迷宫问题(简单bfs+路径打印)

    传送门: http://poj.org/problem?id=3984 迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  9. POJ 3984 迷宫问题

    K - 迷宫问题 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Sta ...

随机推荐

  1. linux设备驱动模型二【转】

    本文转载自:http://blog.csdn.net/u013904227/article/details/51167886 版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[+] ...

  2. Mysql的简单使用(二)

    接上文Mysql的简单使用(一) 字段参数以“(字段名1 数据类型1,字段名2 数据类型2,......)”的形式构建. 关于mysql常用的数据类型,一下是比较常用的几种,想查阅比较详细的资料可以自 ...

  3. TS流解析 四

    一 从TS流开始 数字电视机顶盒接收到的是一段段的码流,我们称之为TS(Transport Stream,传输流),每个TS流都携带一些信息,如Video.Audio以及我们需要学习的PAT.PMT等 ...

  4. P2258 子矩阵(dp)

    P2258 子矩阵 题目描述 给出如下定义: 子矩阵:从一个矩阵当中选取某些行和某些列交叉位置所组成的新矩阵(保持行与列的相对顺序)被称为原矩阵的一个子矩阵. 例如,下面左图中选取第2.4行和第2.4 ...

  5. pip使用豆瓣镜像源

    pip使用豆瓣的镜像源 豆瓣镜像地址: https://pypi.douban.com/simple/ 虽然用easy_install和pip来安装第三方库很方便 他们的原理其实就是从Python的官 ...

  6. 使用Quartz2.2.3做持久化,启动程序后,控制台报错问题

    该错误是由mysql-connector-java.jar版本太低导致. MLog clients using log4j logging. Initializing c3p0-0.9.1.1 [bu ...

  7. 上传Android代码到gerrit服务器

    1. 配置default.xml default.xml是跟Android代码配套的,可参考google Android源码下的default.xml(.repo/manifests/default. ...

  8. Elasticsearch集群状态健康值处于red状态问题分析与解决(图文详解)

      问题详情 我的es集群,开启后,都好久了,一直报red状态??? 问题分析 有两个分片数据好像丢了.   不知道你这数据怎么丢的. 确认下本地到底还有没有,本地要是确认没了,那数据就丢了,删除索引 ...

  9. IIS设置HTTP To HTTPS

    转自: http://www.cnblogs.com/yipu/p/3880518.html 1.购买SSL证书,参考:http://www.cnblogs.com/yipu/p/3722135.ht ...

  10. [转帖]c++ 面试整理

    1. 继承方式 public    父类的访问级别不变 protected    父类的public成员在派生类编程protected,其余的不变 private        父类的所有成员变成pr ...