POJ——3984迷宫问题(BFS+回溯)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14568 | Accepted: 8711 |
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)
这题的状态记录和上一题的不太一样,上一题是不需要回溯的,这题需要回溯,因此不再是简单地记录上一个节点的状态,而是每一个点都记录前驱点,这样根据终点可以回溯到起点。显然每一个点可以向四个方向拓展,因此一个点最多可以成为四个拓展点的前驱点,但这不意味着一个点同时存在四种状态,而是四种状态被分开来压入队列。以前在这点上一直搞不清楚,这题就不会做。由于BFS找到的是最短路,且每一个点只能被访问一次,因此回溯的时候一定是一条最短的路。本来想用另一种方法用下标回溯状态,但是发现每一种状态展开后会叠在前一种后面,需要好几个辅助容器,比较麻烦。还是用一般方法
代码:
- #include<iostream>
- #include<algorithm>
- #include<cstdlib>
- #include<sstream>
- #include<cstring>
- #include<cstdio>
- #include<string>
- #include<deque>
- #include<stack>
- #include<cmath>
- #include<queue>
- #include<set>
- #include<map>
- using namespace std;
- #define INF 0x3f3f3f3f
- #define MM(x) memset(x,0,sizeof(x))
- #define MMINF(x) memset(x,INF,sizeof(x))
- typedef long long LL;
- const double PI=acos(-1.0);
- struct info
- {
- int x;
- int y;
- int prex;
- int prey;
- };
- info direct[4]={{0,1},{0,-1},{1,0},{-1,0}};
- info operator+(const info &a,const info &b)
- {
- info c;
- c.x=a.x+b.x;
- c.y=a.y+b.y;
- return c;
- }
- int pos[5][5],vis[5][5];
- info history[5][5]={{0,0,-1,-1}};
- inline bool check(const info &a)
- {
- if((!vis[a.x][a.y])&&(!pos[a.x][a.y])&&(a.x>=0&&a.y>=0&&a.x<5&&a.y<5))
- return true;
- return false;
- }
- int main(void)
- {
- int i,j;
- for (i=0; i<5; i++)
- {
- for (j=0; j<5; j++)
- {
- scanf("%d",&pos[i][j]);
- }
- }
- queue<info>Q;
- Q.push(history[0][0]);
- vis[history[0][0].x][history[0][0].y]=1;
- while (!Q.empty())
- {
- info now=Q.front();
- Q.pop();
- for (int i=0; i<4; i++)
- {
- info v=now+direct[i];
- v.prex=now.x;
- v.prey=now.y;
- if(check(v))
- {
- Q.push(v);
- vis[v.x][v.y]=1;
- history[v.x][v.y]=v;
- }
- }
- if(now.x==4&&now.y==4)
- break;
- }
- stack<info>ans;
- info k=history[4][4];
- puts("(0, 0)");
- while (k.prex!=-1)
- {
- ans.push(k);
- k=history[k.prex][k.prey];
- }
- while (!ans.empty())
- {
- info t=ans.top();
- printf("(%d, %d)\n",t.x,t.y);
- ans.pop();
- }
- return 0;
- }
POJ——3984迷宫问题(BFS+回溯)的更多相关文章
- 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最短路径的记录和打印问题)
题目链接: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
学会这道水题之后我懂得了不少哈,首先水题也能学到不少知识,尤其像我这样刚入门的小菜鸟,能学到一些小技巧. 然后就是可以从别人的代码里学到不一样的思路和想法. 这题就是求最短的路径,首先想到就是用bfs ...
- POJ - 3984 迷宫问题 bfs解法
#include<stdio.h> #include<string.h> #include<algorithm> #include<stack> usi ...
- POJ 3984 迷宫问题 (BFS + Stack)
链接 : Here! 思路 : BFS一下, 然后记录下每个孩子的父亲用于找到一条路径, 因为寻找这条路径只能从后向前找, 这符合栈的特点, 因此在输出路径的时候先把目标节点压入栈中, 然后不断的向前 ...
- BFS(最短路+路径打印) POJ 3984 迷宫问题
题目传送门 /* BFS:额,这题的数据范围太小了.但是重点是最短路的求法和输出路径的写法. dir数组记录是当前点的上一个点是从哪个方向过来的,搜索+,那么回溯- */ /************* ...
- POJ 3984 迷宫问题(简单bfs+路径打印)
传送门: http://poj.org/problem?id=3984 迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- POJ 3984 迷宫问题
K - 迷宫问题 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Sta ...
随机推荐
- Android 8.0 NotificationChannel 采坑实例
Android O 上Notification的新特性: 通知通道功能 1. 简介: 通知通道功能使开发者管理自己应用的通知成为一个组或者一个通道,用户可以通过通知通道完成设置通知,如:阻止所有通知, ...
- uvm_pkg——老板,打包带走
Thus spake the master programmer: “After three day without programming, life becomes meaningless.” 编 ...
- jmeter中登录和提交收银出现的错误
登录出现的错误 登录界面如图所示: 为了防止登录跳转的问题response code 302的问题,要设置 2.提交收银界面 当系统设置必须传送jison格式时,要在HTTP Header Manag ...
- nginx 编译某个模板的问题./configure: error: SSL modules require the OpenSSL library. You can either do not enable the modules, or install the OpenSSL library into the system, or build the OpenSSL library stati
root@hett-PowerEdge-T30:/usr/local/src/nginx-1.9.8# ./configure --prefix=/usr/local/nginx --add-mod ...
- shell 简单脚本 2
#!/bin/bash source /etc/profile APPLICATIONS_HOME="/cpic/cpicapp/cpic_analy/jars" APPLICAT ...
- java日期操作的工具类时间格式的转换
package cn.itcast.oa.util; import java.text.ParseException; import java.text.SimpleDateFormat;import ...
- 一个batch如何通过一个网络
一个batch下所有的图片一起经过整个网络,不是说一张图片经过网络后再让下一张进入网络,这样一个batch一起通过网络计算速度比一张一张这样快
- Jordan 标准型的实例
将学习到什么 练习一下如何把一个矩阵化为 Jordan 标准型. 将矩阵化为 Jordan 标准型需要三步: 第一步 求出矩阵 \(A \in M_n\) 全部的特征值 \(\lambda_1,\ ...
- apache shiro的工作流程分析
本文基于shiro的web环境,用宏观(也就是不精确)的角度去理解shiro的工作流程,先看shiro官方的一张图. 和应用程序直接交互的对象是Subject,securitymanager为Subj ...
- jQuery-AJAX简介
AJAX是浏览器后台与服务器交换数据的技术,无须加载整个页面的情况下,对页面中的局部进行更新. AJAX=异步的JavaScript与XML(Asynchronous JavaScript and X ...