Permalink
Browse files

Leverage known results for lowestSolutionBound

  • Loading branch information...
jlfwong committed Oct 14, 2011
1 parent 5ec9ffa commit c6057dc1956cfbe89a119aa26ba0a65f50bc3824
Showing with 20 additions and 10 deletions.
  1. +1 −1 spec/solver.spec.coffee
  2. +19 −9 src/grid.coffee
View
@@ -71,7 +71,7 @@ describe 'solve', ->
it 'can solve random shuffles of various sizes', ->
grid = new Grid
size = 5
while size <= 40
while size <= 35
console.warn "Solving 5 random shuffles of #{size} moves"
console.time "Size #{size}"
for i in [1..5]
View
@@ -41,6 +41,12 @@ class @Grid
grid[sourceRow][sourceCol] = 0
nextGrid = new Grid(grid, emptyPos)
number = grid[targetRow][targetCol]
nextGrid._lowerSolutionBound = @lowerSolutionBound() -
rectilinearDistance(number, sourceRow, sourceCol) +
rectilinearDistance(number, targetRow, targetCol)
return nextGrid
applyMoves: (sourceDirections) ->
@@ -59,20 +65,24 @@ class @Grid
from where each number is to where it should
be
###
moveCount = 0
if not @_lowerSolutionBound?
moveCount = 0
for rowNum of @grid
rowNum = parseInt(rowNum, 10)
for colNum of @grid[rowNum]
colNum = parseInt(colNum, 10)
for rowNum of @grid
rowNum = parseInt(rowNum, 10)
for colNum of @grid[rowNum]
colNum = parseInt(colNum, 10)
number = @grid[rowNum][colNum]
number = @grid[rowNum][colNum]
continue if number == 0
continue if number == 0
moveCount += rectilinearDistance number, rowNum, colNum
moveCount += rectilinearDistance number, rowNum, colNum
@_lowerSolutionBound = moveCount
return moveCount
return @_lowerSolutionBound
log: ->
console.log "Empty: #{@emptyPos}"

0 comments on commit c6057dc

Please sign in to comment.