**链接 : ** 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. HDOJ 4705 Y 树形DP

    DP:求出3点构成链的方案数 .然后总方案数减去它 Y Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K ...

  2. 负margin使用注意的一个问题

    在项目实力中经经常使用到负margin 如: <div id="test"> <ul> <li>子元素1</li> <li&g ...

  3. Poj2054 color a tree && [HNOI/AHOI2018]排列

    https://zybuluo.com/ysner/note/1120723 题面 原题 某省选强化题 大致意思是给你一颗树,选父亲后才能选儿子. 每个点对答案的贡献为你在第几次选这个点 × 该点权值 ...

  4. Knights of the Round Table(Tarjan+奇圈)

    http://poj.org/problem?id=2942 题意:n个武士,某些武士之间相互仇视,如果在一起容易发生争斗事件.所以他们只有满足一定的条件才能参加圆桌会议:(1)相互仇视的两个武士不能 ...

  5. bzoj1121[POI2008]激光发射器SZK(结论)

    1121: [POI2008]激光发射器SZK Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 916  Solved: 759[Submit][Sta ...

  6. Educational Codeforces Round 45

    A. 一个小模拟    不解释 //By SiriusRen #include <bits/stdc++.h> using namespace std; long long n,m,a,b ...

  7. ACM_夏天到了,又到了出游的季节

    夏天到了,又到了出游的季节 Time Limit: 2000/1000ms (Java/Others) Problem Description: QWER最近无心打代码,于是带着n套衣服出去浪.但是每 ...

  8. 使用HBuilder新建项目

    依次点击文件→新建→选择Web项目(按下Ctrl+N,W可以触发快速新建(MacOS请使用Command+N,然后左键点击Web项目)) 如上图,请在A处填写新建项目的名称,B处填写(或选择)项目保存 ...

  9. 【转】DOS与linux的断行字符

    转自:http://www.2cto.com/os/201109/104833.html 今天配置linux的dns服务器,在配置的时候,在linux下修改配置文件感觉很麻烦,于是想到把配置文件拿到w ...

  10. 安装cloudermanager时如何正确Configuring TLS Security for Cloudera Manager

    不多说,直接上干货! 参考官网 https://www.cloudera.com/documentation/enterprise/5-2-x/topics/cm_sg_config_tls_secu ...