惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
罗磊的独立博客
The GitHub Blog
The GitHub Blog
V
V2EX
Last Week in AI
Last Week in AI
博客园 - 聂微东
MyScale Blog
MyScale Blog
美团技术团队
L
LangChain Blog
博客园 - Franky
腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园_首页
S
SegmentFault 最新的问题
爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Stack Overflow Blog
Stack Overflow Blog
量子位
小众软件
小众软件
宝玉的分享
宝玉的分享
J
Java Code Geeks
Google DeepMind News
Google DeepMind News
D
Docker
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报

Long Luo's Life Notes

夏至日测地球:利用太阳影子计算地球半径 2009年江西高考数学压轴题:陶平生老师又藏了什么数学机关? 《茶杯里的风暴》读书笔记:从日常生活中的小事,看懂背后的物理学 2008年江西高考数学压轴题详解:为什么它被称为史上最难高考数学题? 太阳温度是怎么计算出来的? 《大象的时间,老鼠的时间》读书笔记:生命节奏背后的数学规律 小港流到哪里去? 如何用一根棍子测出地球有多大?复刻埃拉托色尼的春分实验 2007江苏高考数学第20题解析:一道通向黄金分割数的数列压轴题 Google经典面试题: 鸡蛋应该怎么扔? 2010年江苏高考数学压轴题解析:巧用余弦定理与数学归纳法 2011年清华大学自主招生数学题解析:一道经典数列题的解法与思路 2011年清华大学自主招生数学题解析:一道经典数列题的解法与思路 2006年江西高考理科数学压轴题解析:递推、放缩与不等式结构 2006年江西高考理科数学压轴题解析:递推、放缩与不等式结构 一道初中数学极值题的多种解法:柯西不等式、几何法、函数法详解 扔几个骰子,怎么算出期望?——拼多多校招笔试算法题的数学故事 拼多多校招笔试算法题:一行公式搞定“多多的魔术盒子” 斯特林公式(Stirling's Formula):我一个阶乘表达式,怎么就和圆扯上关系了呢? 我爱做题:2010年江西高考理科数学压轴题 热机的效率上限在哪里?解析卡诺循环(Carnot Cycle) 为什么 2024 年会有 366 天? 数学之美:几何视角下的高斯积分(Gaussian Integral) 从最小二乘法到正态分布:高斯是如何找到失踪的谷神星的? 正态分布(Normal Distribution)公式为什么长这样? 高速公路编号背后的数学密码 2024阿里巴巴全球数学竞赛预选赛试题及解答 库函数 (libm) 是如何计算三角函数值的? payne hanek 归约算法 音乐背后的数学
LeetCode 迷宫问题(The Maze)
2022-10-03 · via Long Luo's Life Notes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class Solution {
public String findShortestWay(int[][] maze, int[] ball, int[] hole) {
int m = maze.length;
int n = maze[0].length;

int[][] distance = new int[m][n];
for (int i = 0; i < m; i++) {
Arrays.fill(distance[i], Integer.MAX_VALUE);
}

Map<Integer, String> map = new HashMap<>();
for (int i = 0; i < m * n; i++) {
map.put(i, "");
}

distance[ball[0]][ball[1]] = 0;
dfs(maze, distance, map, ball[0], ball[1], hole);

String res = map.get(hole[0] * n + hole[1]);
return res.equals("") ? "impossible" : res;
}

private static void dfs(int[][] maze, int[][] distance, Map<Integer, String> map, int x, int y, int[] hole) {
int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
String[] actions = {"u", "d", "l", "r"};

int m = maze.length;
int n = maze[0].length;

for (int i = 0; i < 4; i++) {
int nextX = x;
int nextY = y;
int stepCnt = 0;

String path = map.get(x * n + y);

while (nextX >= 0 && nextX < m && nextY >= 0 && nextY < n && maze[nextX][nextY] == 0
&& !(nextX == hole[0] && nextY == hole[1])) {
nextX += dirs[i][0];
nextY += dirs[i][1];
stepCnt++;
}

if (nextX != hole[0] || hole[1] != nextY) {
nextX -= dirs[i][0];
nextY -= dirs[i][1];
stepCnt--;
}

path += actions[i];

if (distance[nextX][nextY] > distance[x][y] + stepCnt) {
distance[nextX][nextY] = distance[x][y] + stepCnt;
map.put(nextX * n + nextY, path);
if (nextX != hole[0] || nextY != hole[1]) {
dfs(maze, distance, map, nextX, nextY, hole);
}
} else if (distance[nextX][nextY] == distance[x][y] + stepCnt && path.compareTo(map.get(nextX * n + nextY)) < 0) {
map.put(nextX * n + nextY, path);
if (nextX != hole[0] || nextY != hole[1]) {
dfs(maze, distance, map, nextX, nextY, hole);
}
}
}
}
}

All suggestions are welcome. If you have any query or suggestion please comment below. Please upvote👍 if you like💗 it. Thank you:-)