**链接 : ** 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. C# 文件里的类不能进行设计,因此未能为该文件显示设计器

    C# 文件里的类不能进行设计,因此未能为该文件显示设计器 vs 一直打不开设计界面  仅仅能查看代码界面  这时候须要查看  代码中  是不是 从 form    继承 假设不是 窗口类型 改为 fo ...

  2. MyEclipse中加入web项目到tomcat

    假设导入不是在MyEclipse下建立的web项目,想加入到tomcat中时,会显示"No projects are available for deployment to this ser ...

  3. 【Spark】Stage生成和Stage源代码浅析

    引入 上一篇文章<DAGScheduler源代码浅析>中,介绍了handleJobSubmitted函数,它作为生成finalStage的重要函数存在.这一篇文章中,我将就DAGSched ...

  4. Java代码规范_插件_阿里java开发手册

    给大家分享一个阿里巴巴的java开发规范,在日常自动化工作中我们可以参考一下,特别是用java进行coding的同学. 而且还可以利用相应的插件进行代码扫描检测,感兴趣的们可以马上应用到自动化中来. ...

  5. ios4--UIView的常见属性(尺寸和位置)

    // // ViewController.m // 08-UIView的常见属性(尺寸和位置) // // frame:相对于父控件左上角定位 // bounds:改变长宽,左上角是相对于自己 // ...

  6. oc75--不可变字典NSDictionary

    // // main.m // NSDictionary // // #import <Foundation/Foundation.h> int main(int argc, const ...

  7. SQL 字符串处理函数大全

    select语句中只能使用sql函数对字段进行操作(链接sql server),select 字段1 from 表1 where 字段1.IndexOf("云")=1;这条语句不对 ...

  8. Jar包中文乱码问题

    项目上遇用winrar修改替换jar中一个中文文件名后出现jar包解压读取错误问题,被这个问题纠缠了两次,都是现场比较情急的情况,于是就研究一下彻底弄清楚这个问题.中间也网上搜过一些内容,但实际测试不 ...

  9. bzoj1061&&bzoj3256

    http://www.lydsy.com/JudgeOnline/problem.php?id=1061 单纯形... 先开始我不知道对偶,看着代码不知所措,并不能懂他们写的是什么... 单纯形的标准 ...

  10. 【转】Java - printf

    [转自]http://heidian.iteye.com/blog/404632 目前printf支持以下格式:            %c        单个字符            %d     ...