POJ 3984 迷宫问题 (BFS + Stack)
**链接 : ** 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)的更多相关文章
- POJ 3984 - 迷宫问题 - [BFS水题]
题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...
- POJ 3984 迷宫问题 bfs 难度:0
http://poj.org/problem?id=3984 典型的迷宫问题,记录最快到达某个点的是哪个点即可 #include <cstdio> #include <cstring ...
- [POJ 3984] 迷宫问题(BFS最短路径的记录和打印问题)
题目链接:http://poj.org/problem?id=3984 宽度优先搜索最短路径的记录和打印问题 #include<iostream> #include<queue> ...
- POJ - 3984 迷宫问题 bfs解法
#include<stdio.h> #include<string.h> #include<algorithm> #include<stack> usi ...
- 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, ...
- poj 3984 迷宫问题 bfs
学会这道水题之后我懂得了不少哈,首先水题也能学到不少知识,尤其像我这样刚入门的小菜鸟,能学到一些小技巧. 然后就是可以从别人的代码里学到不一样的思路和想法. 这题就是求最短的路径,首先想到就是用bfs ...
- BFS(最短路+路径打印) POJ 3984 迷宫问题
题目传送门 /* BFS:额,这题的数据范围太小了.但是重点是最短路的求法和输出路径的写法. dir数组记录是当前点的上一个点是从哪个方向过来的,搜索+,那么回溯- */ /************* ...
- POJ 3984 迷宫问题(简单bfs+路径打印)
传送门: http://poj.org/problem?id=3984 迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- POJ 3984 迷宫问题
K - 迷宫问题 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Sta ...
随机推荐
- UVA 11748 - Rigging Elections(dfs)
UVA 11748 - Rigging Elections option=com_onlinejudge&Itemid=8&page=show_problem&category ...
- Android---58---初学GPS定位
GPS英文是Global Positioning System 全球定位系统的简称. Android为GPS功能支持专门提供了一个LocationManager,位置管理器.全部GPS定位相关的服务. ...
- ubuntu rdesktop 全屏切换快捷键
rdesktop 全屏之后进行切换 : ctrl + alt +enter
- cgi程序读取post发送的特殊字符,尤其适合于微信公众平台开发中发送被动消息
[问题]用c编写cgi程序怎样取出html表单post来的数据? [分析]html表单post来的数据形如username="zhang"&&password=&q ...
- P2597 [ZJOI2012]灾难 拓扑排序
这个题有点意思,正常写法肯定会T,然后需要优化.先用拓扑排序重构一遍树,然后进行一个非常神奇的操作:把每个点放在他的食物的lca上,然后计算的时候直接dfs全加上就行了.为什么呢,因为假如你的食物的l ...
- 53. 部门信息显示 EXTJS 单击树节点
1. /** * @author sux * @time 2011-1-14 * @desc 部门信息显示 */ deptInfoGridPanel = Ext.extend(Ext.grid.Edi ...
- String Successor(模拟)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3490 题意:给出一个字符串,一个操作次数,每次操作从当前字符串最右边的字符 ...
- zhw大神线段树姿势
; i<; i++) tree[i][]=tree[i][]=i; ; i>=; i--) tree[i][]=tree[i+i][], tree[i][]=tree[i+i+][]; v ...
- SS配置,Brook是什么?,Brook如何配置(Android篇)
很长时间没有更新了,今天给大家分享一下什么是Brook,和SS有什么区别?写的不好,请勿见外,大佬绕过. Brook简单介绍 Brook 是一个高效的 Socks5 代理软件,官方支持Windows. ...
- $P2299 Mzc和体委的争夺战$
\(problem\) #ifdef Dubug #endif #include <bits/stdc++.h> using namespace std; typedef long lon ...