BFS实现迷宫问题
BFS实现迷宫问题
问题描述,要求从起点走到终点,找出最短的距离,要避开障碍
输入描述,输入一个二维数组表示地图,其中等于10就是终点,等于-10就是起点,等于1就是障碍,等于0就是可以走的
代码:
import java.util.LinkedList;
import java.util.Queue;
/**
* @author xuziao
* @date 2021/10/17 19:40
*/
public class BFS {
public static int getShort(int[][] map){
int ans = 0;
int startX = 0;
int startY = 0;
int maxX = map.length - 1;
int maxY = map[0].length - 1;
Queue<Coordinate> coordinateQueue = new LinkedList<>();
for (int x = 0; x < map.length; x++) {
for (int y = 0; y < map[0].length; y++){
if (map[x][y] == -10) {
startX = x;
startY = y;
}
}
}
MovePoint movePoint = new MovePoint(map, maxX, maxY);
Coordinate coordinate = new Coordinate(startX, startY, 0);
coordinateQueue.add(coordinate);
while (true) {
if (coordinateQueue.isEmpty()) {
break;
}
Coordinate point = coordinateQueue.poll();
movePoint.setCoordinate(point);
Coordinate l = movePoint.left();
Coordinate b = movePoint.bottom();
Coordinate t = movePoint.top();
Coordinate r = movePoint.right();
if (l != null) {
if (isAns(l, map)) {
ans = l.step;
break;
}
coordinateQueue.add(l);
}
if (b != null) {
if (isAns(b, map)) {
ans = b.step;
break;
}
coordinateQueue.add(b);
}
if (t != null) {
if (isAns(t, map)) {
ans = t.step;
break;
}
coordinateQueue.add(t);
}
if (r != null) {
if (isAns(r, map)) {
ans = r.step;
break;
}
coordinateQueue.add(r);
}
}
return ans;
}
private static boolean isAns(Coordinate coordinate, int[][] map) {
return map[coordinate.getX()][coordinate.getY()] == 10;
}
public static void main(String[] args) {
//测试数据,-10代表起点,10代表终点,1代表墙
int[][] data = {{-10, 0, 0, 0}, {0, 1, 0, 1}, {0, 0, 1, 10}, {1, 0, 0, 0}};
System.out.println(isAns(new Coordinate(2, 3, 0), data));
//start:(0, 0) end(2, 3)
}
}
class Coordinate {
int x;
int y;
int step = 0;
public Coordinate(int x, int y, int step) {
this.x = x;
this.y = y;
this.step = step;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
class MovePoint{
Coordinate coordinate;
private final int maxX;
private final int maxY;
private final int[][] map;
MovePoint(int[][] map, int maxX, int maxY) {
this.maxY = maxY;
this.maxX = maxX;
this.map = map;
}
public void setCoordinate(Coordinate coordinate) {
this.coordinate = coordinate;
}
public Coordinate top() {
int x = coordinate.getX() - 1;
int y = coordinate.getY();
//判断点是否超出边界
boolean isLegal = x > maxX || x < 0 || y > maxY || y < 0 || map[x][y] == 1;
if (isLegal) {
return null;
} else {
map [x][y] = map[x][y] == 10 ? 10 : 1;
coordinate.step++;
return new Coordinate(x, y, coordinate.step);
}
}
public Coordinate bottom() {
int x = coordinate.getX() + 1;
int y = coordinate.getY();
//判断点是否超出边界
boolean isLegal = x > maxX || x < 0 || y > maxY || y < 0 || map[x][y] == 1;
if (isLegal) {
return null;
} else {
map[x][y] = 1;
coordinate.step++;
return new Coordinate(x, y, coordinate.step);
}
}
public Coordinate right() {
int x = coordinate.getX();
int y = coordinate.getY() + 1;
//判断点是否超出边界
boolean isLegal = x > maxX || x < 0 || y > maxY || y < 0 || map[x][y] == 1;
if (isLegal) {
return null;
} else {
map[x][y] = 1;
coordinate.step++;
return new Coordinate(x, y, coordinate.step);
}
}
public Coordinate left() {
int x = coordinate.getX();
int y = coordinate.getY() - 1;
//判断点是否超出边界
boolean isLegal = x > maxX || x < 0 || y > maxY || y < 0 || map[x][y] == 1;
if (isLegal) {
return null;
} else {
map[x][y] = 1;
coordinate.step++;
return new Coordinate(x, y, coordinate.step);
}
}
}
BFS实现迷宫问题的更多相关文章
- ACM:图BFS,迷宫
称号: 网络格迷宫n行m单位列格组成,每个单元格无论空间(使用1表示),无论是障碍(使用0为了表示).你的任务是找到一个动作序列最短的从开始到结束,其中UDLR同比分别增长.下一个.左.向右移动到下一 ...
- 广度优先搜索(BFS)——迷宫的最短路径
宽度优先搜索按照距开始状态由近到远的顺序进行搜索,因此可以很容易的用来求最短路径,最少操作之类问题的答案. 宽度优先搜索介绍(一篇不错的文章). 题目描述: 给定一个大小为N*M的迷宫.迷宫有通道和墙 ...
- 【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,0,1,0, }; 它表示一个迷 ...
- BFS求解迷宫的最短路径问题
题目:给定一个大小为N*M的迷宫,迷宫由通道('.')和墙壁('#')组成,其中通道S表示起点,通道G表示终点,每一步移动可以达到上下左右中不是墙壁的位置.试求出起点到终点的最小步数.(本题假定迷宫是 ...
- BFS简单迷宫
常见迷宫: 输入迷宫 启点 终点 然后求最短路径 BFS例题 用dist[][]数组来记录 启点到每个点的最短路径 #include <iostream> #include <fst ...
- 用BFS解决迷宫问题
在一个n*n的矩阵里走,从原点(0,0)開始走到终点(n-1,n-1),仅仅能上下左右4个方向走.仅仅能在给定的矩阵里走,求最短步数. n*n是01矩阵,0代表该格子没有障碍.为1表示有障碍物. in ...
- 有关dfs、bfs解决迷宫问题的个人见解
可以使用BFS或者DFS方法解决的迷宫问题! 题目如下: kotori在一个n*m迷宫里,迷宫的最外层被岩浆淹没,无法涉足,迷宫内有k个出口.kotori只能上下左右四个方向移动.她想知道有多少出口是 ...
- Escape(多记一个方向状态的BFS)迷宫逃脱
题意:https://www.nitacm.com/problem_show.php?pid=2266 vis记[x][y][dir]三个状态就行. 引用:https://blog.csdn.net/ ...
- BFS(广度优先搜索)
简介 BFS的过程是首先访问起始结点v,接着访问顶点v的所有未被访问的邻接结点,然后对每个继续进行上述步骤,直到所有结点都被访问过为止,当然,在访问过程中,需要使用一个队列,然后类似二叉树的层次遍历来 ...
随机推荐
- [NOIP2013 提高组] 华容道 P1979 洛谷
[NOIP2013 提高组] 华容道 P1979 洛谷 强烈推荐,更好的阅读体验 经典题目:spfa+bfs+转化 题目大意: 给出一个01网格图,和点坐标x,y空格坐标a,b,目标位置tx,ty要求 ...
- Pytorch 的安装
GPU版本的安装 Windows平台 CPU 版本安装 conda install pytorch torchvision cpuonly -c puython Windows平台需安装VC,需要的联 ...
- WPF中的命令(Command)
这节来讲一下WPF中的命令(Command)的使用. [认识Command] 我们之前说过,WPF本身就为我们提供了一个基础的MVVM框架,本节要讲的命令就是其中一环,通过在ViewModel中声明命 ...
- 【Java虚拟机10】ClassLoader.getSystemClassLoader()流程简析
前言 学习类加载必然离开不了sun.misc.Launcher这个类和Class.forName()这个方法. 分析ClassLoader.getSystemClassLoader()这个流程可以明白 ...
- 【c++ Prime 学习笔记】目录索引
第1章 开始 第Ⅰ部分 C++基础 第2章 变量和基本类型 第3章 字符串.向量和数组 第4章 表达式 第5章 语句 第6章 函数 第7章 类 第 Ⅱ 部分 C++标准库 第8章 IO库 第9章 顺序 ...
- 6. 站在巨人的肩膀学习Java Filter型内存马
本文站在巨人的肩膀学习Java Filter型内存马,文章里面的链接以及图片引用于下面文章,参考文章: <Tomcat 内存马学习(一):Filter型> <tomcat无文件内存w ...
- Java:并发笔记-08
Java:并发笔记-08 说明:这是看了 bilibili 上 黑马程序员 的课程 java并发编程 后做的笔记 7. 共享模型之工具-1 7.1 线程池 1. 自定义线程池 步骤1:自定义拒绝策略接 ...
- [no code][scrum meeting] Alpha 3
项目 内容 会议时间 2020-04-07 会议主题 技术规格说明书review 会议时长 1h30min 参会人员 产品经理+后端技术组长(伦泽标)+OCR竞品调研成员(叶开辉)+架构文档负责(黎正 ...
- 零基础入门非常好的C语言基础资料
C语言程序的结构认识 用一个简单的c程序例子,介绍c语言的基本构成.格式.以及良好的书写风格,使小伙伴对c语言有个初步认识. 例1:计算两个整数之和的c程序: #include main() { in ...
- 2021NOI同步赛
\(NOI\) 网上同步赛 明白了身为菜鸡的自己和普通人的差距 DAY1 \(T1\) 轻重边 [题目描述] 小 W 有一棵 \(n\) 个结点的树,树上的每一条边可能是轻边或者重边.接下来你需要对树 ...