BFS(最短路+路径打印) POJ 3984 迷宫问题
/*
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 迷宫问题的更多相关文章
- POJ 3984 迷宫问题(简单bfs+路径打印)
传送门: http://poj.org/problem?id=3984 迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- (简单) 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, ...
- POJ 3984 迷宫问题
K - 迷宫问题 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- L2-001 紧急救援 (25 分) (最短路+路径打印)
链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805073643683840 题目: 作为一个城市的应急救援队伍的负 ...
- poj 3984 迷宫问题【bfs+路径记录】
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10103 Accepted: 6005 Description ...
- [POJ 3984] 迷宫问题(BFS最短路径的记录和打印问题)
题目链接:http://poj.org/problem?id=3984 宽度优先搜索最短路径的记录和打印问题 #include<iostream> #include<queue> ...
- 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/路径记录/手写队列】
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31428 Accepted: 18000 Description 定义 ...
- POJ 3984 - 迷宫问题 - [BFS水题]
题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...
随机推荐
- 热部署jredel介绍
在进行java web程序开发过程中,经常遇到这种问题,修改一个java文件(*.java),需要重启web服务器(如tomcat,weblogic等),部署项目.而起/停服务器浪费了大量的时间.在小 ...
- POJ 1769_Minimizing maximizer
题意: 一系列m个1~n区间,每个区间固定对某个子区间进行排序,顺序选择若干区间,使最终覆盖所有区间. 分析: computes the length of the shortest subseque ...
- [bzoj3306]树_dfs序_线段树_倍增lca
树 bzoj-3306 题目大意:给定一颗n个节点的树,支持换根.修改点权.查询子树最小值. 注释:$1\le n,q\le 10^5$. 想法: 如果没有换根操作,就是$dfs$序+线段树维护区间最 ...
- Servlet处理日期
以下内容引用自http://wiki.jikexueyuan.com/project/servlet/handling-date.html: 使用Servlet的最重要的优势之一是可以使用核心Java ...
- python 时间四舍五入
假设时间格式为 YYYYMMDDhhmm , 比如201508010001 代表2015年8月1日0点01分. 现在有需求,要求一个start 和一个 end 变量的字符串 都是这种格式的时间. 需要 ...
- restful接口就是url嘛,通过http请求发起访问。那接口进行监控,就可以监控这个restful url嘛
EasyAPI接口管理系统 专注API接口监控,让您的API接口更稳定,与APP更紧密 + 购买监控服务 接口性能分析 分析App对应的API接口请求性能,包含HTTP响应时间.吞吐率.HTTP错误率 ...
- java读取大文本文件
原文:http://blog.csdn.net/k21325/article/details/53886160 小文件当然可以直接读取所有,然后放到内存中,但是当文件很大的时候,这个方法就行不通了,内 ...
- 根据身份证号,取得行政区划的Javascript实现
原文:http://www.cnblogs.com/baibaluo/archive/2011/06/03/2071255.html#2585076 项目里需要一个根据身份证号,取得发证地行政区划的的 ...
- android动态控制组件的位置、大小和新的动画
一.动态设置组件的位置 当中view是须要改变位置的控件,top是须要设制的位置: private static void setLayoutX(View view,int top) { //克隆v ...
- vux 全局使用 loading / toast / alert
1.入口文件 main.js import { LoadingPlugin, ToastPlugin, AlertPlugin } from 'vux' Vue.use(LoadingPlugin); ...