原题链接:https://leetcode.com/articles/the-maze/

这道题目是需要冲会员才能使用的,然而我个穷逼现在还是失业状态根本冲不起。。。以后如果把免费题目都刷完了的话,再来冲会员刷这些题目吧!

我的思路

迷宫类问题首先想到的就是回溯法了,思考+实现用了近 3 个小时,终于又写出了简单粗暴的实现来了:

import java.util.Stack;

/**
* Created by clearbug on 2018/2/26.
*/
public class Solution { public static void main(String[] args) {
Solution s = new Solution();
/**
* 0 0 1 0 0
0 0 0 0 0
0 0 0 1 0
1 1 0 1 1
0 0 0 0 0
*/
int[][] board = {
{0, 0, 1, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 1, 0},
{1, 1, 0, 1, 1},
{0, 0, 0, 0, 0},
};
System.out.println(s.traverse(board, 0, 4, 4, 4)); /**
* 0 0 1 0 0
0 0 0 0 0
0 0 0 1 0
1 1 0 1 1
0 0 0 0 0 */
int[][] board2 = {
{0, 0, 1, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 1, 0},
{1, 1, 0, 1, 1},
{0, 0, 0, 0, 0},
};
System.out.println(s.traverse(board2, 0, 4, 3, 2));
} public Stack<String> traverse(int[][] board, int rowStart, int colStart, int rowDest, int colDest) {
board[rowStart][colStart] = -1; // 标记起始位置已来过 Stack<String> res = new Stack<>();
boolean dfsRes = dfs(board, rowStart, colStart, rowDest, colDest, res);
System.out.println(dfsRes);
return res;
} public boolean dfs(int[][] board, int rowStart, int colStart, int rowDest, int colDest, Stack<String> res) {
if (rowStart == rowDest && colStart == colDest) { // 说明已抵达目的地
return true;
} boolean exist = false; // up
int upRow = rowStart - 1;
while (upRow >= 0 && board[upRow][colStart] < 1) { // board[upRow][colStart] = 0 or board[upRow][colStart] = -1
exist = true;
upRow--;
}
if (exist) {
upRow++;
res.push("up");
if (upRow == rowDest && colStart == colDest) { // 说明已抵达目的地
return true;
}
if (board[upRow][colStart] == -1) { // 说明来过这个位置,目前又循环回来了,所以是死路
res.pop();
} else {
board[upRow][colStart] = -1; // 标记这个位置已来过
if (dfs(board, upRow, colStart, rowDest, colDest, res)) {
return true;
} else {
res.pop();
}
}
} exist = false;
// down
int downRow = rowStart + 1;
while (downRow < board.length && board[downRow][colStart] < 1) { // board[downRow][colStart] = 0 or board[downRow][colStart] = -1
exist = true;
downRow++;
}
if (exist) {
downRow--;
res.push("down");
if (downRow == rowDest && colStart == colDest) { // 说明已抵达目的地
return true;
}
if (board[downRow][colStart] == -1) { // 说明来过这个位置,目前又循环回来了,所以是死路
res.pop();
} else {
board[downRow][colStart] = -1; // 标记这个位置已来过
if (dfs(board, downRow, colStart, rowDest, colDest, res)) {
return true;
} else {
res.pop();
}
}
} exist = false;
// left
int leftCol = colStart - 1;
while (leftCol >= 0 && board[rowStart][leftCol] < 1) {
exist = true;
leftCol--;
}
if (exist) {
leftCol++;
res.push("left");
if (rowStart == rowDest && leftCol == colDest) {
return true;
}
if (board[rowStart][leftCol] == -1) {
res.pop();
} else {
board[rowStart][leftCol] = -1;
if (dfs(board, rowStart, leftCol, rowDest, colDest, res)) {
return true;
} else {
res.pop();
}
}
} exist = false;
// right
int rightCol = colStart + 1;
while (rightCol < board[rowStart].length && board[rowStart][rightCol] < 1) {
exist = true;
rightCol++;
}
if (exist) {
rightCol--;
res.push("right");
if (rowStart == rowDest && rightCol == colDest) {
return true;
}
if (board[rowStart][rightCol] == -1) {
res.pop();
} else {
board[rowStart][rightCol] = -1;
if (dfs(board, rowStart, rightCol, rowDest, colDest, res)) {
return true;
} else {
res.pop();
}
}
} return false;
} }

当然了,可以使用类似骑士游历问题中的预测算法来提高效率。下面就来看看官方解答是怎么的吧!

官方方法一(深入优先搜索)

深度优先搜索,英文全称为 Depth First Search,简称为 DFS。毫无疑问,跟我的思路是一样的,就是代码更加简洁,所以我的解法其实就是深度优先搜索啦!

官方解法二(广度优先搜索)

广度优先搜索,英文全称 Breadth First Search,简称 BFS。不过我思考了下,这种广度优先搜索算法最终是无法求出小球所走过的路径的,只能判断小球最终是否会到达目的地。还是按照惯例抄袭一遍代码就当学会了吧:

import java.util.LinkedList;
import java.util.Stack;
import java.util.Queue; /**
* Created by clearbug on 2018/2/26.
*/
public class Solution { public static void main(String[] args) {
Solution s = new Solution();
/**
* 0 0 1 0 0
0 0 0 0 0
0 0 0 1 0
1 1 0 1 1
0 0 0 0 0
*/
int[][] board = {
{0, 0, 1, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 1, 0},
{1, 1, 0, 1, 1},
{0, 0, 0, 0, 0},
};
System.out.println(s.hasPath(board, new int[]{0, 4}, new int[]{4, 4})); /**
* 0 0 1 0 0
0 0 0 0 0
0 0 0 1 0
1 1 0 1 1
0 0 0 0 0 */
int[][] board2 = {
{0, 0, 1, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 1, 0},
{1, 1, 0, 1, 1},
{0, 0, 0, 0, 0},
};
System.out.println(s.hasPath(board2, new int[]{0, 4}, new int[]{3, 2}));
} public boolean hasPath(int[][] maze, int[] start, int[] dest) {
boolean[][] visited = new boolean[maze.length][maze[0].length];
visited[start[0]][start[1]] = true; int[][] dirs = {
{0, 1},
{0, -1},
{-1, 0},
{1, 0}
}; Queue<int[]> queue = new LinkedList<>();
queue.add(start); while (!queue.isEmpty()) {
int[] s = queue.remove();
if (s[0] == dest[0] && s[1] == dest[1]) {
return true;
}
for (int[] dir : dirs) {
int x = s[0] + dir[0];
int y = s[1] + dir[1];
while (x >= 0 && y >= 0 && x < maze.length && y < maze[0].length && maze[x][y] == 0) {
x += dir[0];
y += dir[1];
}
if (!visited[x - dir[0]][y - dir[1]]) {
queue.add(new int[]{x - dir[0], y - dir[1]});
visited[x - dir[0]][y - dir[1]] = true;
}
}
} return false;
} }

490. The Maze的更多相关文章

  1. [LeetCode] 490. The Maze 迷宫

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...

  2. LeetCode 490. The Maze

    原题链接在这里:https://leetcode.com/problems/the-maze/ 题目: There is a ball in a maze with empty spaces and ...

  3. [LC] 490. The Maze

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...

  4. 【LeetCode】490. The Maze 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...

  5. [LeetCode] 499. The Maze III 迷宫 III

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...

  6. [LeetCode] 505. The Maze II 迷宫 II

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...

  7. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  8. 算法与数据结构基础 - 广度优先搜索(BFS)

    BFS基础 广度优先搜索(Breadth First Search)用于按离始节点距离.由近到远渐次访问图的节点,可视化BFS 通常使用队列(queue)结构模拟BFS过程,关于queue见:算法与数 ...

  9. 算法与数据结构基础 - 深度优先搜索(DFS)

    DFS基础 深度优先搜索(Depth First Search)是一种搜索思路,相比广度优先搜索(BFS),DFS对每一个分枝路径深入到不能再深入为止,其应用于树/图的遍历.嵌套关系处理.回溯等,可以 ...

随机推荐

  1. dubbo报com.alibaba.dubbo.remoting.TimeoutException: Waiting server-side response timeout. start time: 2020-03-28 23:08:50.342, end time: 2020-03-28 23:08:51.344,

    当进行debug 启动项目报 dubbo remotiong timeout ,默认1一秒,要在spring配置文件中,dubbo配置中dubbo:service配置timeout属性,如下图配置10 ...

  2. harbor越权漏洞(CVE-2019-16097)

    漏洞介绍 这个漏洞可以在注册发送post包时,加入has_admin_role:true就可以直接注册成为管理员,下图可以看看user的结构: 有很多属性,此处我们关注的是"HasAdmin ...

  3. JavaScript 简版-菜鸟中的菜鸟

    JavaScript 简介 JavaScript 是互联网上最流行的脚本语言,这门语言可用于 HTML 和 web,更可广泛用于服务器.PC.笔记本电脑.平板电脑和智能手机等设备. JavaScrip ...

  4. 关于Backus-Naur Form巴克斯诺尔范式和扩展巴克斯范式的知识点和相关词语中英文对照

    巴克斯诺尔范式的相关词语中英文对照和知识点 syntax 语法 强调的是编程语言的组形式,例如一个句子中会包含表达式.陈述还有各种单元等等 semantics 语义 强调的是这个编程语言的实际含义,例 ...

  5. 副业收入是我做程序媛的3倍,工作外的B面人生

    到“程序员”,多数人脑海里首先想到的大约是:为人木讷.薪水超高.工作枯燥…… 然而,当离开工作岗位,撕去层层标签,脱下“程序员”这身外套,有的人生动又有趣,马上展现出了完全不同的A/B面人生! 不论是 ...

  6. Chisel3-Intellij IDEA安装Scala插件

    https://mp.weixin.qq.com/s/xTk5ucvSNuwsh8C6E362cg   后续开启RISC-V开发相关内容.   RISC-V开发推荐使用Chisel编程语言.Chise ...

  7. 面试题: hashset如何保证值不会被重复的

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 众所周知,HashSet 的值是不可能被重复的,在业务上经常被用来做数据去重的操作,那么,其内部究竟是怎 ...

  8. Java实现 蓝桥杯 算法提高 三进制数位和

    算法提高 三进制数位和 时间限制:1.0s 内存限制:256.0MB 提交此题 问题描述 给定L和R,你需要对于每一个6位三进制数(允许前导零),计算其每一个数位上的数字和,设其在十进制下为S. 一个 ...

  9. Java实现 蓝桥杯VIP 算法训练 完数

    问题描述 一个数如果恰好等于它的因子之和,这个数就称为"完数".例如,6的因子为1.2.3,而6=1+2+3,因此6就是"完数".又如,28的因子为1.2.4. ...

  10. 运行npm run start 提示primordials is not defined

    下载https://github.com/ant-motion/editor-list 执行 npm install npm start gulp构建时报错. 原因:安装gulp版本与node版本不兼 ...