views:

554

answers:

1

I've heard that the 8-puzzle problem can be tackled via BFS, but I don't understand how. I wanna know the intermediate steps that I need to get from a board like this:

3 1 2
6 4 5
0 7 8

to

1 2 3
4 5 6
7 8 0

Are the intermediate steps "levels" on a BFS search?

By the way, this is basic homework, I don't care about optimality.

+2  A: 

this is pretty much a template for any BFS search

function next_boards(board)
   yields a set of reachable in one move from the current board

queue = [start_board]

while true:
   current = queue.pop()
   if current = goal: break

   queue.push for all next_boards(current)

note we're not doing anything fancy like checking for cycles or anything. if we were, change queue to a stack, and you get DFS.

Jimmy
Yep basically use a FIFO queue and expand every node to each possible next node. If I'm not mistaken, the 8-puzzle can take close to a million nodes expanded in the worst case scenario, so make sure your state object is small or allow your virtual memory to grow in order to avoid memory errors. The average case should be >100K nodes expanded.
NickLarsen