Skip to content

Commit eb3b57d

Browse files
committed
2 parents 20feb7b + 0b78d76 commit eb3b57d

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
fun isReachableAtTime(sx: Int, sy: Int, fx: Int, fy: Int, t: Int): Boolean {
3+
if (sx == fx && sy == fy && t == 1) return false
4+
val dstX = Math.abs(fx - sx)
5+
val dstY = Math.abs(fy - sy)
6+
val diag = minOf(dstY, dstX) //diagonal movement
7+
val line = maxOf(dstY, dstX) - diag //straight line movement
8+
val shortest = diag + line
9+
return t >= shortest
10+
}
11+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
fun minimumMoves(grid: Array<IntArray>): Int {
3+
// Base Case
4+
var empties = 0
5+
repeat(3) { r ->
6+
repeat(3) { c ->
7+
if (grid[r][c] == 0) empties++
8+
}
9+
}
10+
if (empties == 0) return 0
11+
12+
var res = Int.MAX_VALUE
13+
repeat(3) { emptyR -> repeat(3) { emptyC ->
14+
if (grid[emptyR][emptyC] == 0) {
15+
repeat(3) { spareR -> repeat(3) { spareC ->
16+
val distance = abs(spareR - emptyR) + abs(spareC - emptyC)
17+
if (grid[spareR][spareC] > 1) {
18+
grid[spareR][spareC]--
19+
grid[emptyR][emptyC]++
20+
res = min(res, distance + minimumMoves(grid)) //backtrack
21+
grid[spareR][spareC]++
22+
grid[emptyR][emptyC]--
23+
}
24+
} }
25+
}
26+
} }
27+
return res
28+
}
29+
}

0 commit comments

Comments
 (0)