【CodeWars】Path Finder #2: shortest path
题意
题目链接:https://www.codewars.com/kata/path-finder-number-2-shortest-path
Task
You are at position [0, 0] in maze NxN and you can only move in one of the four cardinal directions (i.e. North, East, South, West). Return the minimal number of steps to exit position [N-1, N-1] if it is possible to reach the exit from the starting position. Otherwise, return false in JavaScript/Python and -1 in C++/C#/Java.Empty positions are marked .. Walls are marked W. Start and exit positions are guaranteed to be empty in all test cases.
题目意思很清楚,就是求解迷宫的最短路径长度,迷宫格式如下,点表示路,W表示墙,入口是(0,0)位置,出口是最后一行的最后一个点:
".W.\n“”
".W.\n""..."
代码
迷宫有很多求解方法,我采用的是BFS,代码中搜索路径经过的点,其中(x,y)是左边,steps表示到达当前位置经过的步数,当搜索到迷宫出口时放回对应的steps就可以了,需要注意的是BFS使用了队列,需要标记某个位置是否将会被访问,也就是是否已经加入到队列中(这里使用steps作为标记,当steps为0时表示未加入队列,这样可以省去一个变量),需要在坐标入队时将对应的steps标记为上一步steps+1,而不是在坐标出队的时候标记访问,否则会重复搜索很多的位置,导致时间复杂度爆炸
import java.util.LinkedList;
import java.util.Queue;public class Finder {public static int pathFinder(String maze) {maze = maze.replaceAll("\n","");StringBuilder stringBuilder = new StringBuilder(maze);int n = (int) Math.sqrt(maze.length());stringBuilder.replace(0,1,"W");Queue<Position> positionQueue = new LinkedList<Position>() {{offer(new Position(0,0,0));}};int[][] dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};while (!positionQueue.isEmpty()) {Position position = positionQueue.poll();int i = position.x;int j = position.y;if (i == n - 1 && j == n - 1) return position.steps;for (int k = 0; k < dir.length; k++) {int posX = i + dir[k][0];int posY = j + dir[k][1];if (posX < 0 || posY < 0 || posX >= n || posY >= n || stringBuilder.charAt(posX * n + posY) == 'W')continue;positionQueue.offer(new Position(posX,posY,position.steps + 1));stringBuilder.replace(posX * n + posY,posX * n + posY + 1,"W");}}return -1;}static class Position{int x;int y;int steps;public Position(int x, int y, int steps) {this.x = x;this.y = y;this.steps = steps;}}
}
【CodeWars】Path Finder #2: shortest path相关推荐
- 4kyu Path Finder #2: shortest path
4kyu Path Finder #2: shortest path 题目背景: Task You are at position [0, 0] in maze NxN and you can onl ...
- AOJ GRL_1_B: Shortest Path - Single Source Shortest Path (Negative Edges) (Bellman-Frod算法求负圈和单源最短路径)
题目链接: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_B Single Source Shortest Path ( ...
- 【Python】Python3.7.3 - sys.path 模块搜索路径详解
文章目录 系统参考 sys.path - 模块搜索路径 用户站点目录(user site directory) 安装包到用户站点目录 第三方站点目录 site.py介绍 python启动选项 PYTH ...
- 【LeetCode】124. Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- 【clickhouse】MergeTree storage require data path INCOORRECT_FILE_NAME
文章目录 1.概述 1.概述 docker下建什么样的表都报这个,这个应哪里设置 安装的没问题,后来docker下ck不能建表的原因找到了[表情] ck接管了mysql库 mysql只有select权 ...
- 【Mac】Macbook Finder 从当前位置快速打开终端
第一步.新建文件保存成 .scpt 文件 set cmd to "" tell application "Finder"set theWindow to win ...
- 【CodeWars】 Pete, the baker
CodeWars 里的5kyu题目 Pete,the baker 题目说明: Description: Pete likes to bake some cakes. He has some recip ...
- 【Codewars】Simple Pig Latin
Codewars里的 5kyu Kata. 题目说明: Move the first letter of each word to the end of it, then add "ay&q ...
- 【Codewars】All Inclusive?
Codewars里的 7kyu Kata. 题目说明: Description: Input: a string strng an array of strings arr Output of fun ...
- 【Codewars】<7kyu> Vowel Count
一.题目: Return the number (count) of vowels in the given string. 这道题要实现的是返回字符串中的元音个数(a,e,i,o,u) 二.例子: ...
最新文章
- javarxtx串口接收时数据会分成好几段_【STM32】串口通信基本原理(超基础、详细版)...
- Enumeration接口
- 《实施Cisco统一通信管理器(CIPT2)》一1.6 拨号计划方面面临的挑战
- Ubuntu15.0.4下Torch安装教程
- WDS 自动化部署安装 win2008 服务器实验
- Python 使用@property对属性进行数据规范性校验
- [设计模式] 15.Command 命令模式
- 将list对象转换为QuerySet对象
- fusion构建器代码语法_构建器模式:适用于代码,适用于测试
- WinForm程序启动时不显示主窗体的实现方法
- 解决复制项目后名称不改变的问题:org.eclipse.wst.common.component
- laydate 周_宝来屋:5天168万!海盐周大生新店如何赢过竞品实现可持续化客流-广告随意贴...
- 现实世界的 Windows Azure:Davide Bedin讲述aKite零售管理解决方案
- uni-app微信小程序——商城(6)——我的主页
- UE4 粒子特效基础学习 ——仿英雄联盟亮牌效果(图标触发特效的制作)
- 亚马逊条码打印_使用亚马逊的条形码扫描仪轻松从手机上购买任何东西
- 学神经网络需要什么基础,深度神经网络怎么用
- php遍历数组查询数据库,php如何遍历数据库查询数组
- SpringCloud+CloudAlibaba微服务初阶入门
- 魔方(10)金字塔魔方、金字塔二重奏魔方
热门文章
- 关于Python爬虫常见的面试题
- 群晖docker安装cms_群晖docker安装蚂蚁笔记安装教程
- 黑客白皮书:如何成为一名黑客(附FAQ)
- 使用yigo遇到的小问题
- vue.js中created()与activated()的个人使用理解
- 计算机考研复试-计算机组成原理
- JVM(6)之 二次标记
- IDEA felix osgi项目搭建(1)
- spring加载流程之AnnotatedBeanDefinitionReader
- Hexo搭建个人博客(十五)| 酒香也怕巷子深,让百度收录你的站点