题目传送门

 /*
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. 斯特林(Stirling)公式 求大数阶乘的位数

    我们知道整数n的位数的计算方法为:log10(n)+1n!=10^m故n!的位数为 m = log10(n!)+1 lgN!=lg1+lg2+lg3+lg4+lg5+................. ...

  2. Mycat集群方案收集(待实践)

    先收集,后续再实践. 我想,市面上开源方案中,涉及到高可用和负载均衡的部署,无论是哪一个产品应用,都基本离不开LVS+Keepalived+HAProxy+Nginx等等. 下面是收集的教程: htt ...

  3. Servlet的调试

    以下内容引用自http://wiki.jikexueyuan.com/project/servlet/debugging.html: 测试/调试Servlet始终是困难的.Servlets往往涉及大量 ...

  4. 什么是Wiki?

    Wiki一词来源于夏威夷语的“wee kee wee kee”, 发音wiki, 原本是“快点快点”的意思,被译为“维基”或“维客”.一种多人协作的写作工具.Wiki站点可以有多人(甚至任何访问者)维 ...

  5. shell脚本变量的参数

    https://stackoverflow.com/questions/5163144/what-are-the-special-dollar-sign-shell-variables mkdir f ...

  6. 三元表达式之理解/jquery源代码分析之$.inArray实现

    每次看到三元表达式就会惶惶然分不清怎样读,正如语文中的断句一样,jquery源代码中的三元表达式更是不知怎样断句. 附jquery中的inArray实现. 大家熟悉jquery的应该都不陌生inArr ...

  7. 2016/1/14 java随机数生成

    1.Math库里的static(静态)方法random() 该方法的作用是产生0到1之间(包括0,但不包括1)的一个double值. double rand = Math.random(); 2.通过 ...

  8. HNOI模拟 Day3.25 By Yqc

    怕老婆 [问题描述] 有一天hzy9819,来到了一座大城市拥有了属于他自己的一双滑板鞋.但是他还是不满足想要拥有属于自己的一栋楼,他来到了一条宽敞的大道上,一个一个记录着这些楼的层数以方便自己选择. ...

  9. 简单脱壳教程笔记(7)---手脱PECompact2.X壳

    本笔记是针对ximo早期发的脱壳基础视频教程.整理的笔记.本笔记用到的工具下载地址: http://download.csdn.net/detail/obuyiseng/9466056 简单介绍: F ...

  10. Junit 测试基础

    /** * 1.测试函数以     @Test  注解, 函数名可以根据测试内容自定义但返回值必须是void,不能有参数 * 2.assertEquals(arg0,arg1); 用来判断期待值是否和 ...