Skip to content

Commit a9f4564

Browse files
committed
Fix A* algorithm bug
1 parent 4f8dc7a commit a9f4564

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

‎src/main/kotlin/grid/dynamic_programming/CherryPickup_II.kt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package grid.dynamic_programming
22

33
class CherryPickup_II {
4+
private val NEGATIVE_INFINITY = -1e8.toInt()
45
data class State(val r: Int, val c1: Int, val c2: Int)
56

67
fun cherryPickup(grid: Array<IntArray>): Int {
@@ -9,14 +10,13 @@ class CherryPickup_II {
910

1011
// Column offsets for robots
1112
val dirs = intArrayOf(-1, 0, 1)
12-
1313
fun pickupCherry(row: Int, robot1Col: Int, robot2Col: Int)
1414
= if (robot1Col == robot2Col) grid[row][robot1Col] else grid[row][robot1Col] + grid[row][robot2Col]
1515

1616
fun solve(r: Int, c1: Int, c2: Int): Int =
1717
cache.getOrPut(State(r, c1, c2)) {
1818
when {
19-
c1 !in 0 until cols || c2 !in 0 until cols -> Int.MIN_VALUE
19+
c1 !in 0 until cols || c2 !in 0 until cols -> NEGATIVE_INFINITY
2020
r == rows - 1 -> pickupCherry(r, c1, c2)
2121
else -> {
2222
pickupCherry(r, c1, c2) + dirs.maxOf { dc1 ->

0 commit comments

Comments
 (0)