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, ...
随机推荐
- Daily,一个入门级的 React Native 应用
Daily,一个React-Native写的android app. 下拉刷新获取:图片.诗句.言语.音乐.乐评.雨声.知乎日报.历史上的今天. 可以说是一个入门级的React-Native应用. 项 ...
- Appium+python自动化(三十三)- 环境(超详解)
简介 在前边所有涉及启动app的时候有这样一行代码driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps),很多小 ...
- virtualenv使用和virtualenvwrapper使用笔记
virtualenv使用笔记 1.安装 pip install virtualenv 2.创建虚拟环境 virtualenv env //对于python2.7,该虚拟环境env必须在英文目录路径下 ...
- net core Webapi基础工程搭建(七)——小试AOP及常规测试_Part 1
目录 前言 拦截器 异常拦截器 测试结果 身份验证拦截器 测试 小结 补充 2019-07-31 前言 一天天不知道怎么过的,但确实挺忙,事赶事不带停那种,让我感觉跟在流水线干活一样,忙活的事差不多了 ...
- 循环 与 分支语句 和 字符函数库cctype 文件简单处理
循环 for循环 while循环 do while循环 通常,入口条件循环比出口条件循环好,因为循环开始前对条件进行检查 c++11基于范围的for循环 对数组(或容器类,如:vector和a ...
- VR、AR、MR、CR 与 AI与SaaS、CRM、MRP与B2B、B2C、C2C、O2O、P2P
一.VR.AR.MR.CR VR ( Virtual Reality ),虚拟现实 AR(Augmented Reality),增强现实 MR(Mix Reality),混合现实 CR(Cinema ...
- springboot整合webservice采用CXF技术
转载自:https://blog.csdn.net/qq_31451081/article/details/80783220 强推:https://blog.csdn.net/chjskarl/art ...
- Vue+ElementUI项目使用webpack输出MPA
目录 Vue+ElementUI项目使用webpack输出MPA 一. 需求分析 二. 原方案分析 三. 多页面改造3步走 四. 小结 Vue+ElementUI项目使用webpack输出MPA 示例 ...
- odoo t标签用法
在odoo中,通过QWeb来对模板进行渲染后加载到浏览器中,而模板中有许多的标签来定制各种需求变化,在这里记录学习过程中碰到的标签定义,以方便查询. 模板中的标签统一都是以"t-" ...
- odoo模板中的t-标签用法
在OpenERP中,通过QWeb来对模板进行渲染后加载到浏览器中,而模板中有许多的标签来定制各种需求变化,在这里记录学习过程中碰到的标签定义,以方便查询. 模板中的标签统一都是以"t-&qu ...
