题目传送门

 /*
BFS:额,这题的数据范围太小了。但是重点是最短路的求法和输出路径的写法。
dir数组记录是当前点的上一个点是从哪个方向过来的,搜索+,那么回溯-
*/
/************************************************
Author :Running_Time
Created Time :2015-8-4 9:02:06
File Name :POJ_3984.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int MAXN = ;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + ;
int a[MAXN][MAXN];
bool vis[MAXN][MAXN];
int dir[MAXN][MAXN];
int step[MAXN][MAXN];
int dx[] = {-, , , };
int dy[] = {, , -, };
int n = , m = ; bool judge(int x, int y) {
if (x < || x > n || y < || y > m || a[x][y] == ) return false;
return true;
} void print_path(void) {
int x = n, y = m; vector<pair<int, int> > ans;
while (dir[x][y] != -) {
ans.push_back (make_pair (x, y));
int px = x, py = y;
x -= dx[dir[px][py]]; y -= dy[dir[px][py]];
}
int sz = (int) ans.size ();
printf ("(0, 0)\n");
for (int i=sz-; i>=; --i) {
printf ("(%d, %d)\n", ans[i].first, ans[i].second);
}
} void BFS(void) {
memset (vis, false, sizeof (vis));
memset (step, INF, sizeof (step));
memset (dir, -, sizeof (dir));
queue<pair<int, int> > Q; Q.push (make_pair (, )); vis[][] = true;
step[][] = ;
while (!Q.empty ()) {
int x = Q.front ().first, y = Q.front ().second; Q.pop ();
for (int i=; i<; ++i) {
int tx = x + dx[i], ty = y + dy[i];
if (!judge (tx, ty)) continue;
if (vis[tx][ty] && step[tx][ty] <= step[x][y] + ) continue;
if (tx == n && ty == m) {
dir[tx][ty] = i; print_path (); return ;
}
dir[tx][ty] = i; step[tx][ty] = step[x][y] + ;
Q.push (make_pair (tx, ty)); vis[tx][ty] = true;
}
}
} int main(void) { //POJ 3984 迷宫问题
for (int i=; i<; ++i) {
for (int j=; j<; ++j) {
scanf ("%d", &a[i][j]);
}
}
BFS (); return ;
}

BFS(最短路+路径打印) POJ 3984 迷宫问题的更多相关文章

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

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

  2. (简单) POJ 3984 迷宫问题,BFS。

    Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, ...

  3. POJ 3984 迷宫问题

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

  4. L2-001 紧急救援 (25 分) (最短路+路径打印)

    链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805073643683840 题目: 作为一个城市的应急救援队伍的负 ...

  5. poj 3984 迷宫问题【bfs+路径记录】

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10103   Accepted: 6005 Description ...

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

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

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

  8. POJ 3984 迷宫问题【BFS/路径记录/手写队列】

    迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31428 Accepted: 18000 Description 定义 ...

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

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

随机推荐

  1. 热部署jredel介绍

    在进行java web程序开发过程中,经常遇到这种问题,修改一个java文件(*.java),需要重启web服务器(如tomcat,weblogic等),部署项目.而起/停服务器浪费了大量的时间.在小 ...

  2. LOJ#541. 「LibreOJ NOIP Round #1」七曜圣贤

    有一辆车一开始装了编号0-a的奶茶,现有m次操作,每次操作Pi在[-1,b),若Pi为一个未出现过编号的奶茶,就把他买了并装上车:若Pi为一个在车上的奶茶,则把他丢下车:否则,此次操作为捡起最早丢下去 ...

  3. 2.3 comparator(比较器)

    1.comparator是java的一种机制,用来帮助我们给相同对象的不同属性排序 2.Comparable接口,是一个对象本身就已经支持自比较所需要实现的接口,如String,Integer自己就已 ...

  4. MVC view页面需要多个model,复杂网页的处理

    需求描述 一个比较复杂的页面,界面中包含的元素数据来自于许多个有关联或者无关联的表,然后我们要做的就是将数据呈现在界面上. 10年前大概都是这么干的 直接写一个复杂的SQL语句,返回一个包含所需数据的 ...

  5. JSP标准标签库(JSTL)

    JSTL:JSP Standard Tag Library:JSP标准标签库 以下内容引用自http://wiki.jikexueyuan.com/project/jsp/standard-tag-l ...

  6. 关于python内存管理里的引用计数算法和标记-清楚算法的讨论

    先记录于此,后续有时间再深究吧: 1.https://www.zhihu.com/question/33529443 2.http://patshaughnessy.net/2013/10/30/ge ...

  7. sonar做代码检测时如何忽略一些代码文件

    1.管理员登录sonar 2.如图 一条规则配置一个,不要填写逗号或者分号分割的多个规则

  8. Android: Mac无法找到Android SDK问题

    通过brew cask install android-sdk后,Intellij Idea中设置Android SDK路径失败,解决方法如下: /usr/local/Caskroom/android ...

  9. 如何快速掌握plc或工控机与其他设备的modbus通讯协议?包括格式与实际过程 RT,本人从事工控行业多年,对于PLC与触摸屏也算比较熟悉,唯独对这个通讯协议比较难理解,请教高人指导,从什么地方开始下手,或者是说如何正确理解报文格式或正确写入

    Modbus协议是OSI模型的第七层的应用层通讯协议,定义了不同类型设备间交换信息方式,以及信息的格式. Modbus的工作方式是请求/应答,每次通讯都是主站先发送指令,可以是广播,或是向特定从站的单 ...

  10. NA交换①

    常用的交换设备:     交换机(ASIC)和网桥(Brigde):     交换机的三种转发方式:     直通式(Cut-Through):一旦检测到MAC即转发,速度快但是无法保证准确性:    ...