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 ...
随机推荐
- Socket 长连接 短连接 心跳 JAVA SOCKET编程
简单解释就是: 短连接:建立连接,发送数据包.关闭连接 长连接:建立连接.发送数据包,发送心跳包,发送数据包,发送心跳包.发送心跳包. ..... 所以又频繁的数据收发的话.短连接会频繁创建TCP连接 ...
- SQL经典面试题集锦
1.问题背景 (1)学生表(学号,姓名,年龄,性别) student(S#,Sname,Sage,Ssex) (2)课程表(课程编号,课程名称,教师编号) course(C#,Cname,T#) (3 ...
- JXLS-----JXLS导出Excel
- android Activity初次的启动的时候播放声音
代码例如以下: private MediaPlayer mMediaPlayer; mMediaPlayer = new MediaPlayer(); mMediaPlayer = MediaPlay ...
- 简析LCD1602液晶驱动及在Arduino上的实例实现
这几日在倒腾新到的Arduino,比起普通单片机来,感觉写程序太简单了.不过和外设打交道还是没那么容易,比如今天要说的看似简单的LCD1602液晶,却费了我一整天才基本搞懂,不过还是有一个小问题没有实 ...
- POJ3090 Visible Lattice Points 欧拉函数
欧拉函数裸题,直接欧拉函数值乘二加一就行了.具体证明略,反正很简单. 题干: Description A lattice point (x, y) in the first quadrant (x a ...
- handbook/CentOS/使用免费SSL证书让网站支持HTTPS访问.md
- Ruby Range类
创建: 2017/09/30
- Codeforces 455D 分块+链表
题意: 给定一个长度为 N 的序列两种操作1 l r 将[l,r]的数向右循环移位 2 l r 询问[l,r]内有多少个数等于 k其中 N,Q≤105,ai≤N 强制在线 思路: 1. 每块用一个链表 ...
- OI——不后悔的两年
NOI2014,悲惨的考跪,99+170+130 399 Cu滚粗.最终签到了复旦的一本,还算是有点结果吧.(其实我一开始就想读复旦我会说?)回首这两年,就像一场梦一样,从一无所知的小白到进入省队再到 ...