Skip to content

Commit c6057dc

Browse files
committed
Leverage known results for lowestSolutionBound
1 parent 5ec9ffa commit c6057dc

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

spec/solver.spec.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ describe 'solve', ->
7171
it 'can solve random shuffles of various sizes', ->
7272
grid = new Grid
7373
size = 5
74-
while size <= 40
74+
while size <= 35
7575
console.warn "Solving 5 random shuffles of #{size} moves"
7676
console.time "Size #{size}"
7777
for i in [1..5]

src/grid.coffee

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ class @Grid
4141
grid[sourceRow][sourceCol] = 0
4242

4343
nextGrid = new Grid(grid, emptyPos)
44+
45+
number = grid[targetRow][targetCol]
46+
nextGrid._lowerSolutionBound = @lowerSolutionBound() -
47+
rectilinearDistance(number, sourceRow, sourceCol) +
48+
rectilinearDistance(number, targetRow, targetCol)
49+
4450
return nextGrid
4551

4652
applyMoves: (sourceDirections) ->
@@ -59,20 +65,24 @@ class @Grid
5965
from where each number is to where it should
6066
be
6167
###
62-
moveCount = 0
68+
if not @_lowerSolutionBound?
69+
70+
moveCount = 0
71+
72+
for rowNum of @grid
73+
rowNum = parseInt(rowNum, 10)
74+
for colNum of @grid[rowNum]
75+
colNum = parseInt(colNum, 10)
6376

64-
for rowNum of @grid
65-
rowNum = parseInt(rowNum, 10)
66-
for colNum of @grid[rowNum]
67-
colNum = parseInt(colNum, 10)
77+
number = @grid[rowNum][colNum]
6878

69-
number = @grid[rowNum][colNum]
79+
continue if number == 0
7080

71-
continue if number == 0
81+
moveCount += rectilinearDistance number, rowNum, colNum
7282

73-
moveCount += rectilinearDistance number, rowNum, colNum
83+
@_lowerSolutionBound = moveCount
7484

75-
return moveCount
85+
return @_lowerSolutionBound
7686

7787
log: ->
7888
console.log "Empty: #{@emptyPos}"

0 commit comments

Comments
 (0)