题目传送门

 /*
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. CF901C. Bipartite Segments

    n<=300000,m<=300000的图,图上只有奇环,q<=300000个询问每次问:一个区间内有多少个子区间,满足只保留编号在该区间的点以及他们之间的边,可以构成一个二分图. ...

  2. hnuun 11544 小明的烦恼——找字符串(求环形字符串的最小最大字典序)

    http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11544&courseid=0 最小最大表示法: 求环 ...

  3. CentOS 7防火墙服务FirewallD指南

    CentOS 7防火墙服务FirewallD指南 作者:chszs,未经博主同意不得转载.经许可的转载需注明作者和博客主页:http://blog.csdn.net/chszs 防火墙是一种位于内部网 ...

  4. SaltStack学习系列之State安装Nginx+PHP环境

    目录结构 |-- pillar | |-- nginx | | `-- nginx.sls #nginx变量(key:value) | `-- top.sls `-- salt|-- init #初始 ...

  5. Python学习系列之反射

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

  6. socket 由浅入深系列------ 原理(一)

    来自:网络整理 个人觉得写一个网络应用程序没有是一件非常easy的事.其实,我们刚開始的时候总觉得的原则: 建立------>连接套接字------->接受一个连接---->发送数据 ...

  7. linux 下使用genymotion

    在官网下载genymotion http://www.genymotion.cn/ 然后进行下面操作 1.假设本机没有virtualbox 下载一个  能够通过指令 sudo apt-get inst ...

  8. Android之——多线程下载演示样例

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46883927 一.概述 说到Android中的文件下载.Android API中明 ...

  9. Cocos2d-X中的ProgressTimer

     ProgressTimer即进度条,进度条在游戏开发中运用很广泛,比如在一些格斗游戏中,显示血液的变化,还有游戏载入进度,等都离不开进度条 Cocos2d-X中使用CCProgressTimer ...

  10. SQL Server 海量数据查询代码优化以及建议

    1.应尽量避免在  where  子句中对字段进行   null  值推断,否则将导致引擎放弃使用索引而进  行全表扫描,如:     select id from t where num is nu ...