poj3984迷宫问题(DFS广搜)
迷宫问题
Time Limit: 1000MS
Memory Limit: 65536K
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
一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。
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)
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
bool ism[5][5];
int a[5][5];
int dx[4]={0, 1, 0, -1};
int dy[4] = { 1, 0, -1, 0 };
struct Node{
int x;
int y;
int s;
short l[30];
};
bool judge(int x, int y){
if (x < 0 || x >= 5 || y < 0 || y >= 5)
return true;
if (ism[x][y])
return true;
if (a[x][y] == 1)
return true;
return false;
}
Node bfs(){
queue<Node> q;
Node cur, next;
cur.x = 0;
cur.y = 0;
cur.s = 0;
ism[cur.x][cur.y] = true;
q.push(cur);
while (!q.empty()){
cur = q.front();
q.pop();
if (cur.x == 4 && cur.y == 4)
return cur;
int i, nx, ny;
for (i = 0; i < 4; i++){
nx = cur.x + dx[i];
ny = cur.y + dy[i];
if (judge(nx, ny))
continue;
next = cur;
next.x = nx;
next.y = ny;
next.s = cur.s+1;
next.l[cur.s] = i;
q.push(next);
}
}
return cur;
}
int main(){
int i, j;
for (i = 0; i < 5; i++){
for (j = 0; j < 5; j++){
scanf("%d", &a[i][j]);
}
}
memset(ism, 0, sizeof(ism));
Node ans = bfs();
int x, y;
x = 0, y = 0;
for (i = 0; i<= ans.s; i++){
printf("(%d,%d)\n", x, y);
x += dx[ans.l[i]];
y += dy[ans.l[i]];
}
return 0;
}
poj3984迷宫问题(DFS广搜)的更多相关文章
- poj 3984:迷宫问题(广搜,入门题)
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7635 Accepted: 4474 Description ...
- hrbust 1621 迷宫问题II 广搜
题目链接:http://acm.hrbust.edu.cn/vj/index.php?/vj/index.php?c=&c=contest-contest&cid=134#proble ...
- poj3984迷宫问题(dfs+stack)
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35426 Accepted: 20088 Descriptio ...
- 算法学习笔记(六) 二叉树和图遍历—深搜 DFS 与广搜 BFS
图的深搜与广搜 复习下二叉树.图的深搜与广搜. 从图的遍历说起.图的遍历方法有两种:深度优先遍历(Depth First Search), 广度优先遍历(Breadth First Search),其 ...
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
- 图的基本操作(基于邻接矩阵):图的构造,深搜(DFS),广搜(BFS)
#include <iostream> #include <stdio.h> #include <cstdlib> #include <cstring> ...
- 什么时候用深搜(dfs)什么时候用广搜(bfs)(转)
1.BFS是用来搜索最短径路的解是比较合适的,比如求最少步数的解,最少交换次数的解,因为BFS搜索过程中遇到的解一定是离根最近的,所以遇到一个解,一定就是最优解,此时搜索算法可以终止.这个时候不适宜使 ...
- 图的基本操作(基于邻接表):图的构造,深搜(DFS),广搜(BFS)
#include <iostream> #include <string> #include <queue> using namespace std; //表结点 ...
- POJ3984 BFS广搜--入门题
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20816 Accepted: 12193 Descriptio ...
随机推荐
- 关于word粘贴图片无法显示的原因
今天在进行word文档粘贴图片编辑操作的时候,老是无法完整的显示图片.或者干脆就不显示图片,以为是qq截图的图片格式无法粘贴到word文档里面,用Photoshop更改图片的格式仍然无法显示(将png ...
- Elasticsearch学习笔记(七)document小结
一.生成document id 1.自动生成document id 自动生成的id,长度为20个字符,URL安全,base64编码,GUID,分布式系统并行生成时不可能 ...
- linux pwd命令 显示当前所在路径
pwd 显示当前所在路径 [root@MongoDB ~]# pwd /root
- NSRunLoop 在mac command line tool上的部分运用
首先RunLoop相关博客参考这篇https://blog.csdn.net/lengshengren/article/details/12905627. 最近开发了一个mac上的命令行工具,我在主线 ...
- phpstudy2018 安装xdebug扩展
第一步:查看PHP版本信息 第二步:到xdebug下载页去下载最新的版本(注意:要下载PHP对应版本) 第三步:把扩展php_xdebug-2.7.0alpha1-7.2-vc15-nts.dll放到 ...
- 基于Servlet的MVC模式用户登录实例
关于MVC模式的简单解释 M Model,模型层,例如登录实例中,用于处理登录操作的类: V View,视图层,用于展示以及与用户交互.使用html.js.css.jsp.jQuery等前端技术实现: ...
- ESP8266小知识与注意事项
小知识 1. 什么是"512+512".“1024+1024”? 当ESP8266支持FOTA(无线升级)时,会给系统做个备份,当升级失败时,使之不至于死机.所以flash会被分割 ...
- C#中的as关键字
你能进到这篇文章…… 首先你可能明白类似于 double num=0;int m=(int) num; 或者你明白 int m=209;String str=m.toString();或者说你甚至见过 ...
- Appium+Python3+iOS真机环境搭建
Appium 是一个自动化测试开源工具,支持 iOS 平台和 Android 平台上的原生应用,web 应用和混合应用. 本次环境配置相关:macOS:10.13.4Appium-desktop:1. ...
- Kettle使用MySQL作为资源库报错解决方法
使用Kettle5.0.1,安装的是MySQL5.1.JDK1.6,在\data-integration\lib目录里添加mysql的JDBC包mysql-connector-java-5.0.8-b ...