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 ...
随机推荐
- 线性表->链式存储->线形链表(单链表)
文字描述: 为了表示前后两个数据元素的逻辑关系,对于每个数据元素,除了存储其本身的信息之外(数据域),还需存储一个指示其直接后继的信息(即直接后继的存储位置,指针域). 示意图: 算法分析: 在单链表 ...
- API 自动化框架
API 自动化框架 个人认为接口自动化测试使用python语言编写更加简单,但所有接口自动化项目代码的思维都是一样的 1.项目包结构 1.case:存放用例数据的包,将所有用例数据以配置文件形式传入 ...
- FindVisualChild
public static List<T> FindVisualChild<T>(DependencyObject obj) where T : DependencyObjec ...
- 使用Gadget 做usb鼠标键盘设备
使用Gadget 做usb鼠标键盘设备 感谢TI社区提供的好帮助啊!http://e2e.ti.com/support/arm/sitara_arm/f/791/p/571771/2103409?pi ...
- 爬虫下载QQ音乐:获取所有歌手-每个歌手的专辑-每个专辑里的歌曲
# coding=utf-8 # !/usr/bin/env python ''' author: dangxusheng desc : 稍微有点难度,需要多次请求获取key date : 2018- ...
- CentOS7、REHL7的firewalld防火墙使用简单说明
title: CentOS7.REHL7的firewalld防火墙使用简单说明 categories: Linux tags: - Linux timezone: Asia/Shanghai date ...
- VMware运行gazebo,关于vmw_ioctl_command error Invalid argument错误
运行rviz 时报 VMware: vmw_ioctl_command error 无效的参数. 这个错误. 或者 运行gazebo,关于vmw_ioctl_command error Inval ...
- webpack(5) -开发环境
使用 source map (仅用于开发环境) 当 webpack 打包源代码时,可能会很难追踪到 error(错误) 和 warning(警告) 在源代码中的原始位置.例如,如果将三个源文件(a.j ...
- mac Robotframework执行时报错Robot Framework installation not found.
虽然已经装了,但一直报错 ,版本是3.1.1 最新版 ➜ ~ pip install robotframework DEPRECATION: Python 2.7 will reach the en ...
- TypeError: Fetch argument 0.484375 has invalid type <class 'numpy.float32'>, must be a string or Tensor. (Can not convert a float32 into a Tensor or Operation.)
报错: TypeError: Fetch argument 0.484375 has invalid type <class 'numpy.float32'>, must be a str ...