Skip to content

jackcoded/8puzzleAstar

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 

Repository files navigation

How to compile and run python ./puzzle.py

Goal state [1,2,3,8,0,4,7,6,5] 1 2 3 8 0 4 7 6 5

Heuristic Function Description: Manhattan Heuristic counts the total distance for all blocks from source to destination. How it works: My board is a just a single list of numbers, for example [1,2,3,4,5,6,7,8,0] Because it’s just a list of numbers, it is difficult to count rows and columns when there isn’t any. By using the mod 3 technique for columns and divide 3 and round down for rows, we can check if they’re in the same row and columns and the distance between them. First get the number board, discard 0 for admissible heuristic for i in board: if i is not 0:

Getting displacement displacement = abs(goalState.index(i) - board.index(i)) Check if same row if abs(int(math.floor(goalState.index(i) / 3)) - int(math.floor(board.index(i) / 3))) == 0 distance += displacement Check if same col if abs(goalState.index(i) % 3 - board.index(i) % 3) == 0 distance += int(math.floor(displacement / 3)) if not same col or row col = displacement % 3 row = int(math.floor(displacement / 3)) distance += row + col if not same col or row and both src and goal are both edges added to the previous distance as well if abs(goalState.index(i) % 3 - board.index(i) % 3) == 2 and displacement % 3 == 1: distance += 2 This is all looped through each number on the board till return the total distance  

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages