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, ...
随机推荐
- 初学html总结
2019-08-17 17:58:49 html:超文本标记语言,用于网页结构的搭建 html语言构成:由标签.属性.属性值构成 标签:" < "后面第一个单词 属性:标签后 ...
- Spring Context 你真的懂了吗
今天介绍一下大家常见的一个单词 context 应该怎么去理解,正确的理解它有助于我们学习 spring 以及计算机系统中的其他知识. 1. context 是什么 我们经常在编程中见到 contex ...
- SpringMVC源码分析4:DispatcherServlet如何找到正确的Controller
SpringMVC是目前主流的Web MVC框架之一. 我们使用浏览器通过地址 http://ip:port/contextPath/path进行访问,SpringMVC是如何得知用户到底是访问哪个 ...
- MySQL之主从同步
一.主从同步概念 1.1 什么是主从同步? MySQL 主从同步是指将数据从一个 MySQL 数据库服务器主节点复制到一个或多个从节点.MySQL 默认采用异步复制方式,这样从节点不用一直访问主服务器 ...
- Markdown表格宽度调整
Markdown 表格默认宽度是根据内容来的,如果某一列内容很长的话会将其他列的宽度占用导致显示样式很丑.我们可以在表格前增加 CSS 样式来限制列的宽度: <style> table t ...
- The Best Path(HDU5883)[欧拉路]2016青岛online
题库链接:http://acm.hdu.edu.cn/showproblem.php?pid=5883 欧拉回路裸题,第一次接触欧拉路的我是真的长见识了^-^ 懂了欧拉路这道题就是没什么问题了,欧拉路 ...
- Git 上传本地项目到Github
前提: 安装Git 注册并拥有Github账号 目录: 初始化本地目录位Git仓库 Github上创建仓库 本地生成SSH key,并添加到Github上 本地项目管理Github上远程项目 详细步骤 ...
- Python从入门到精通之环境搭建
本章内容: Windows系统环境搭建 Linux系统环境搭建 Mac OS系统环境搭建 一.下载python安装包 下载地址:https://www.python.org/downloads/ 二. ...
- k8s集群部分常见问题处理
目录 部分常见问题处理 Coredns CrashLoopBackOff 导致无法成功添加工作节点的问题 添加工作节点时提示token过期 kubectl 执行命令报“The connection t ...
- 选择排序&冒泡排序&折半查找
//选择排序 void test2(int a[],int len){ //每次找出一个最小值,最小值依次与原数组交换位置,通过下标来完成交换,最小值下标每次都在变,变量存储 // 假如第一个是 ...
