迷宫问题

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广搜)的更多相关文章

  1. poj 3984:迷宫问题(广搜,入门题)

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7635   Accepted: 4474 Description ...

  2. hrbust 1621 迷宫问题II 广搜

    题目链接:http://acm.hrbust.edu.cn/vj/index.php?/vj/index.php?c=&c=contest-contest&cid=134#proble ...

  3. poj3984迷宫问题(dfs+stack)

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35426   Accepted: 20088 Descriptio ...

  4. 算法学习笔记(六) 二叉树和图遍历—深搜 DFS 与广搜 BFS

    图的深搜与广搜 复习下二叉树.图的深搜与广搜. 从图的遍历说起.图的遍历方法有两种:深度优先遍历(Depth First Search), 广度优先遍历(Breadth First Search),其 ...

  5. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  6. 图的基本操作(基于邻接矩阵):图的构造,深搜(DFS),广搜(BFS)

    #include <iostream> #include <stdio.h> #include <cstdlib> #include <cstring> ...

  7. 什么时候用深搜(dfs)什么时候用广搜(bfs)(转)

    1.BFS是用来搜索最短径路的解是比较合适的,比如求最少步数的解,最少交换次数的解,因为BFS搜索过程中遇到的解一定是离根最近的,所以遇到一个解,一定就是最优解,此时搜索算法可以终止.这个时候不适宜使 ...

  8. 图的基本操作(基于邻接表):图的构造,深搜(DFS),广搜(BFS)

    #include <iostream> #include <string> #include <queue> using namespace std; //表结点 ...

  9. POJ3984 BFS广搜--入门题

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20816   Accepted: 12193 Descriptio ...

随机推荐

  1. 使用maxwell实时同步mysql数据到kafka

    一.软件环境: 操作系统:CentOS release 6.5 (Final) java版本: jdk1.8 zookeeper版本: zookeeper-3.4.11 kafka 版本: kafka ...

  2. Java基础语法入门01

    Java基础语法入门01 学习java你要先进行去了解JDK,JRE,JVM JDK Java开发工具包 JRE Java语言开发的运行环境 JVM Java虚拟机,用于Java语言的跨平台所用. 当 ...

  3. esp32固件烧录

    正常使用IO0置空即可.烧录时需要en引脚接高电平,IO0接地,使用乐鑫的烧录工具烧录即可.注意如果启用wifi,供电一定要不小于500ma,普通的usb转ttl模块无法满足wifi启动的,被坑的好惨 ...

  4. C# 图片识别

    项目需要识别图片上的信息,网上搜索试了Asprise-OCR.Microsoft Office Document Imaging(Office 2007) 组件实现两种方式,后者可以识别中文等其他语言 ...

  5. java实现每个单词首字母大写

    /** * 每个单词第一个字母大写 * @param str * @return */ public static String toUpperFirstCode(String str) { Stri ...

  6. 【mybatis】mybatis中 <if test=>等于的条件怎么写

  7. springcloud第五步:使用Zuul搭建服务接口网关

    路由网关(zuul) 什么是网关 Zuul的主要功能是路由转发和过滤器.路由功能是微服务的一部分,比如/api/user转发到到user服务,/api/shop转发到到shop服务.zuul默认和Ri ...

  8. ios APNS注册失败 本地push

    - (void)addLocalNotice:(NSString *)titlepush { if (@available(iOS 10.0, *)) { UNUserNotificationCent ...

  9. VS2017 + EF6连接MySql

    VS2017 + EF6连接MySql   原地址:https://blog.csdn.net/mzhifa/article/details/80999105 VS2017 + EF6连接MySql ...

  10. Oauth2.0安全问题浅谈

    大家如果对Oauth还不是很了解可以先看下这篇文章https://www.cnblogs.com/maoxiaolv/p/5838680.html 我这篇博客主要是总结一下安全测试过程中遇到Oauth ...