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, ...
随机推荐
- Elasticsearch6.x和7.x版本常用插件汇总
elasticsearch插件汇总 基于es 7.3版本试用. 一.安全插件 1.x-pack a.介绍 包括安全(x-pack-security),监视(x-pack-watcher),警报(x-p ...
- RedHat 6.5换源
https://wenku.baidu.com/view/5b87fb42c77da26924c5b03b.html
- Nginx安装之源码安装
nginx部署 1. 安装依赖 yum install gcc gccc++ pcre pcre-devel zlib zlib-devel openssl openssl-devel-y 2. 下载 ...
- 一文道尽JavaScript 20年的发展史
作者介绍:Andrew Montalenti是Parse.ly的CTO, 一个长期的Python爱好者,以及初创公司和其他项目的创始人. 原文链接:https://amontalenti.com/ ...
- ubuntu 用户无法/循环登陆的问题
安装oracle数据库的时候需要新建用户oracle,使用的命令为:useradd oracle.这样做直接创建了用户oracle,但并没有在home目录下创建oracle用户的文件.在log out ...
- 【原创】Linux PSCI框架
背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: Kernel版本: ...
- odoo12从零开始:一、安装odoo运行环境(mac)
写在前面: 接触odoo已经两年多了,在大学做课程设计的时候,无意间了解到odoo这个erp框架,当时的odoo在国内还默默无闻,我也不曾想过自己毕业后会从事到odoo框架的相关开发工作中来.两年多的 ...
- light 1205 - Palindromic Numbers(数位dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1205 题解:这题作为一个数位dp,是需要咚咚脑子想想的.这个数位dp方程可能不 ...
- Codeforces 416D Population Size
Population Size 题意: 一共n个数, 每个-1都可以变成一个正数, 现在要求最少数目的等差子序列,并且在这个子序列必须要连着截取一段,不能分开截取. 样例1: 8 6 4 2 1 4 ...
- CodeForces Educational Codeforces Round 51 (Rated for Div. 2)
A:Vasya And Password 代码: #include<bits/stdc++.h> using namespace std; #define Fopen freopen(&q ...
