POJ - 3984 迷宫问题 (搜索)
Problem 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,
0, 0, 0, 1, 0,
};
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
Input
Output
Sample Input
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
Sample Output
(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4) 也是kuangbin搜索专题里面的,说起这道题,也是满满的恶意,先看图吧
整整花了一个小时去找到底哪里PE了。
题目思路很明确,BFS或者DFS都可以,但其实这个题目没必要DFS,简单BFS标记一下前驱就行了,何为前驱,就是说你走到了下一步你上一步 是从哪里走来的,然后用优先队列保证每次优先走距离右下角最近的路。那么问题来了,如何输出,因为我们存的是前驱,所以可以先把所有前驱 入栈,再依次出栈输出就行了,但最开始我想到了另一种更好的方法,因为我发现
ostringstream outt;
outt << << << ; string s = outt.str();
reverse(s.begin(), s.end());
cout << s << endl;
利用ostringstream流,最后倒过来就可以实现直接顺序输出了,提交PE。找了半天发现是<<endl;倒序后变成先输出了,那么我第一个不加endl,再次提交PE、PE、PE、PE。到这里我觉得可能是ostringstream影响缓冲区,不能这样,改成栈模拟,还是PE =7=,直到最后我发现,(a, b)中间 逗号后面 有个空格 /微笑/微笑。然后改了两种方法都AC了;
AC代码
#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <iomanip>
#include <map>
#include <stack>
#include <deque>
#include <queue>
#include <vector>
#include <set>
#include <list>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <iterator>
#include <cmath>
#include <bitset>
#include <ctime>
#include <fstream>
#include <limits.h>
#include <numeric> using namespace std; #define F first
#define S second
#define mian main
#define ture true #define MAXN 1000000+5
#define MOD 1000000007
#define PI (acos(-1.0))
#define EPS 1e-6
#define MMT(s) memset(s, 0, sizeof s)
typedef unsigned long long ull;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef stringstream sstm;
const int INF = 0x3f3f3f3f; int mp[][],vis[][];
int fx[][] = {,,-,,,-,,};
vector< pair< int, pair<int,int> > >bj(); //记录前驱和位置
ostringstream outt;
class cmp{ //优先队列使得曼哈顿距离小的优先出队
public:
bool operator() (const pair<int,int>a,const pair<int,int>b) const{
int ax = - a.F + - a.S;
int bx = - b.F + - b.S;
return ax > bx;
}
}; void bfs(){
priority_queue< pair<int,int>,vector< pair<int,int> >, cmp >q;
//queue< pair<int,int> >q;
q.push(make_pair(,));
vis[][] = ;
while(!q.empty()){
pair<int,int>nx = q.top();
q.pop();
//cout << nx.F << " " << nx.S << endl;
if( - (nx.F + nx.S) == ){
bj[].F = nx.F*+nx.S;
bj[].S.F = , bj[].S.S = ;
break;
} for(int i = ; i < ; i++){
int nxx = nx.F + fx[i][];
int nxy = nx.S + fx[i][];
if(nxx < || nxx > || nxy < || nxy > || mp[nxx][nxy] == || vis[nxx][nxy])
continue;
vis[nxx][nxy] = ;
q.push(make_pair(nxx,nxy));
bj[nxx*+nxy].F = nx.F*+nx.S;
bj[nxx*+nxy].S.F = nxx, bj[nxx*+nxy].S.S = nxy;
}
}
int nex = ;
/* //这是用栈模拟的方式
stack<int>p;
while(nex){
p.push(nex);
nex = bj[nex].F;
}
p.push(0);
while(!p.empty()){
nex = p.top();
p.pop();
cout << "(" << bj[nex].S.F << ", " << bj[nex].S.S << ")"<< endl;
}
*/
while(){ //反向输出到ostringstream中
//cout << nex << endl;
if(nex == ){
outt << ")" << bj[nex].S.S << " ," << bj[nex].S.F << "(";
break;
}
outt << ")" << bj[nex].S.S << " ," << bj[nex].S.F << "(" << "\n";
nex = bj[nex].F;
} } int main(){
ios_base::sync_with_stdio(false);
cout.tie();
cin.tie();
fill(vis[],vis[]+*,);
for(int i = ; i < ; i++){
for(int j = ; j < ; j++){
cin>>mp[i][j];
}
}
bfs();
string s = outt.str();
reverse(s.begin(),s.end()); //再次逆序
cout << s << endl; return ;
}
其他就是一个简单的BFS,值得注意就是优先队列的使用,当然用DFS也行 ,而且DFS就不需要这么多繁杂的逆序了,直接记录从起点到终点的路径输出就好了。
POJ - 3984 迷宫问题 (搜索)的更多相关文章
- BFS(最短路+路径打印) POJ 3984 迷宫问题
题目传送门 /* BFS:额,这题的数据范围太小了.但是重点是最短路的求法和输出路径的写法. dir数组记录是当前点的上一个点是从哪个方向过来的,搜索+,那么回溯- */ /************* ...
- POJ 3984 迷宫问题
K - 迷宫问题 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- [POJ 3984] 迷宫问题(BFS最短路径的记录和打印问题)
题目链接:http://poj.org/problem?id=3984 宽度优先搜索最短路径的记录和打印问题 #include<iostream> #include<queue> ...
- POJ 3984 迷宫问题(简单bfs+路径打印)
传送门: http://poj.org/problem?id=3984 迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- poj 3984 迷宫问题(dfs)
题目链接:http://poj.org/problem?id=3984 思路:经典型的DFS题目.搜索时注意剪枝:越界处理,不能访问处理. 代码: #include <iostream> ...
- POJ - 3984迷宫问题(最短路径输出)
题目链接:http://poj.org/problem?id=3984 题目: 迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submiss ...
- POJ 3984 - 迷宫问题 - [BFS水题]
题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...
- POJ 3984 迷宫问题 bfs 难度:0
http://poj.org/problem?id=3984 典型的迷宫问题,记录最快到达某个点的是哪个点即可 #include <cstdio> #include <cstring ...
- 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, ...
随机推荐
- UI 组件 | Toggle
Toggle(复选框)组件 Toggle 是一个 CheckBox,当它和 ToggleGroup 一起使用的时候,可以变成 RadioButton. 创建 Toggle 组件 层级管理器右击-> ...
- 二、Ansible的Ad-hoc介绍篇
一.什么是Ad-hoc 称为临时命令,简单说,就是在命令行界面,直接通过一条ansible命令,去指定主机执行指定指令,功能有限 例如:ansible localhost -m command -a ...
- 【在 Nervos CKB 上做开发】Nervos CKB脚本编程简介[2]:脚本基础
CKB脚本编程简介[2]:脚本基础 原文作者:Xuejie 原文链接:Introduction to CKB Script Programming 2: Script 本文译者:Shooter,Jas ...
- springcloud项目配置拓展从本地config目录加载
本文受阿里开源的Nacos启发,应用启动后从Nacos服务加载配置到应用中,想着本地开发的时候加载配置能否从本地存储中加载,这样也能加快开发效率 首先我们来看下SpringCloud项目应用Nacos ...
- Leetcode之二分法专题-275. H指数 II(H-Index II)
Leetcode之二分法专题-275. H指数 II(H-Index II) 给定一位研究者论文被引用次数的数组(被引用次数是非负整数),数组已经按照升序排列.编写一个方法,计算出研究者的 h 指数. ...
- PHP 递归读取无限级分类
/** * [获取第一级分类] * 20170829 * * @return array */ public function getCateList(){ $this->catelog = [ ...
- MSIL实用指南-Action的生成和调用
MSIL实用指南-Action的生成和调用 System.Action用于封装一个没有参数没有返回值的方法.这里生成需要Ldftn指令. 下面讲解怎生成如下的程序. class ActionTest ...
- redis数据结构、持久化、缓存淘汰策略
Redis 单线程高性能,它所有的数据都在内存中,所有的运算都是内存级别的运算,而且单线程避免了多线程的切换性能损耗问题.redis利用epoll来实现IO多路复用,将连接信息和事件放到队列中,依次放 ...
- hdu 2615 Division(暴力)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2615 题解:挺简单的暴力枚举,小小的分治主要是看没人写题解就稍微写一下 #include <io ...
- CodeForces Educational Codeforces Round 51 (Rated for Div. 2)
A:Vasya And Password 代码: #include<bits/stdc++.h> using namespace std; #define Fopen freopen(&q ...
