490. The Maze
原题链接: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的更多相关文章
- [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 ...
- LeetCode 490. The Maze
原题链接在这里:https://leetcode.com/problems/the-maze/ 题目: There is a ball in a maze with empty spaces and ...
- [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 ...
- 【LeetCode】490. The Maze 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...
- [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 ...
- [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 ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- 算法与数据结构基础 - 广度优先搜索(BFS)
BFS基础 广度优先搜索(Breadth First Search)用于按离始节点距离.由近到远渐次访问图的节点,可视化BFS 通常使用队列(queue)结构模拟BFS过程,关于queue见:算法与数 ...
- 算法与数据结构基础 - 深度优先搜索(DFS)
DFS基础 深度优先搜索(Depth First Search)是一种搜索思路,相比广度优先搜索(BFS),DFS对每一个分枝路径深入到不能再深入为止,其应用于树/图的遍历.嵌套关系处理.回溯等,可以 ...
随机推荐
- 使用css实现loading的加载
使用css实现loading的加载的效果图 html代码 <div id="caseVerteClaire"> <div id="transform&q ...
- 用python做时间序列预测一:初识概念
利用时间序列预测方法,我们可以基于历史的情况来预测未来的情况.比如共享单车每日租车数,食堂每日就餐人数等等,都是基于各自历史的情况来预测的. 什么是时间序列? 时间序列,是指同一个变量在连续且固定的时 ...
- jchdl - RTL实例 - MOS6502 ALU
https://mp.weixin.qq.com/s/nMxYVC2djk7DdAforerZPA 使用jchdl RTL实现MOS6502 CPU的ALU. 参考链接 https://git ...
- 【Flutter组件终结篇】332个组件 658页PDF
老孟导读:历时1年的时间,整理完成了330+组件的详细用法,不仅包含UI组件,还包含了功能性的组件. 虽然整理了 330+的组件基本用法,但并不是让你每一个都学习一遍,任何技术基本都是掌握 20%就可 ...
- Java实现 LeetCode 815 公交路线(创建关系+BFS)
815. 公交路线 我们有一系列公交路线.每一条路线 routes[i] 上都有一辆公交车在上面循环行驶.例如,有一条路线 routes[0] = [1, 5, 7],表示第一辆 (下标为0) 公交车 ...
- Java实现 LeetCode 813 最大平均值和的分组 (DFS+DP记忆化搜索)
813. 最大平均值和的分组 我们将给定的数组 A 分成 K 个相邻的非空子数组 ,我们的分数由每个子数组内的平均值的总和构成.计算我们所能得到的最大分数是多少. 注意我们必须使用 A 数组中的每一个 ...
- 循序渐进VUE+Element 前端应用开发(7)--- 介绍一些常规的JS处理函数
在我们使用VUE+Element 处理界面的时候,往往碰到需要利用JS集合处理的各种方法,如Filter.Map.reduce等方法,也可以设计到一些对象属性赋值等常规的处理或者递归的处理方法,以前对 ...
- npm run dev启动项目,electron提示throw new Error('Electron failed to install correctly, please delete node_modules/electron and try installing again')
npm run dev 项目,提示 throw new Error('Electron failed to install correctly, please delete node_modules/ ...
- 温故知新-Mysql的体系结构概览&sql优化步骤
文章目录 Mysql的体系结构概览 连接层 服务层 引擎层 存储层 存储引擎 存储引擎概述 存储引擎特性![存储引擎特性对比](https://img-blog.csdnimg.cn/20200510 ...
- Spark Streaming,Flink,Storm,Kafka Streams,Samza:如何选择流处理框架
根据最新的统计显示,仅在过去的两年中,当今世界上90%的数据都是在新产生的,每天创建2.5万亿字节的数据,并且随着新设备,传感器和技术的出现,数据增长速度可能会进一步加快. 从技术上讲,这意味着我们的 ...