题目传送门

 /*
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. POJ1276 Cash Machine

    Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %lld & %llu Description A Bank pla ...

  2. hosts.allow和hosts.deny文件

    之前想通过外部主机访问自己主机上的VMWare虚拟机,使用了VMWare的NAT端口映射,经过一番尝试,算是成功了,总结一下. VMWare NAT端口映射就可以将虚拟机上的服务映射成自己主机上的端口 ...

  3. linux 磁盘阵列

    1.独立磁盘冗余阵列 (RAID) 2.RAID级别: raid0 扩展卷 (条带卷) 至少一块硬盘 具有较高的存储性能 数据请求多块硬盘并行应答 连续数据分散到多个磁盘存储 ,一块磁盘坏掉所有文件就 ...

  4. 调整JVM内存大小

    首次运行公司项目,出现了内存溢出,具体出现java.lang.OutOfMemoryError: PermGen space和java.lang.OutOfMemoryError:GC overhea ...

  5. HTTPS 是如何保证安全的?

    每当我们讨论到信息安全的时候,我们最长接触到的信息加密传输的方式莫过于 HTTPS 了,当我们浏览器地址栏闪现出绿色时,就代表着这个网站支持 HTTPS 的加密信息传输方式,并且你与它的连接确实被加密 ...

  6. Java利用jacob实现文档格式转换

    实现文档格式之间的转换,我使用的是jacob-1.7版本,需要jacob.jar来调用activex控件,本机需安装WPS/office,还需要jacob.jar以及jacob.dll 其中:    ...

  7. 纯css3实现按钮的 hover 和 active 时颜色的明暗变化效果

    效果:在任意HTML标签上增加样式类 f-color-control 就可以为此元素增加hover和avtive时颜色的变化; 代码如下: <!DOCTYPE html> <html ...

  8. 咏南 DELPHI DATASNAP LINUX中间件

    咏南 DATASNAP LINUX中间件   咏南 DATASNAP LINUX中间件,一套源码,同时支持WINDOWS和LINUX操作系统. 基于DELPHI 10.2 TOKYO开发 使用FIRE ...

  9. 怎样把UCos-ii_在STM32上的移植

    下载代码 stm32 标准外设库是 stm32 全系列芯片的外设驱动,有了它能够大大加速我们 开发 stm32. 首先从 st 公司的站点下载最新的 stm32 标准外设库,写本文时最新的版本号是 V ...

  10. Python学习系列之反射

    反射的定义 根据字符串的形式去某个对象中操作成员 根据字符串的形式去某个对象中寻找成员 根据字符串的形式去某个对象中设置成员 根据字符串的形式去某个对象中删除成员 根据字符串的形式去某个对象中判断成员 ...