I'm using Breadth First Search to solve a rush hour game. It works fine, but it takes really long on difficult boards. I am using a taboo list to avoid states I already discovered, to avoid insane memory usage and improve the run time.
I think this taboo list is the main cause of the long run time. It does drastically improve the time compared to normal BFS, but it's still too slow. Currently I'm using a normal list (C#'s List
and the List.Contains
method). I'm sure there are better options.
I'm storing my boards as a list of Cars + a width, height and target point (where your car should end up). a Car is stored as 2 points (top-left and bottom-right) that completely describe the car (since they can only be placed horizontally or vertically).
Some things I can think of:
- A trie
- Something with hash codes
- Huge Dictionaries (?)
What is a good/the best data structure for my problem? Thanks for the help.
Edit 1: Pseudocode (X is the taboo list type):
void Solve(Board b)
Queue q = {b};
X taboo = {b};
while (q not empty)
Board next = q.Dequeue();
foreach (Board succ in next.Successors)
if (succ.IsSolved)
PrintSolution();
return;
if (!taboo.Contains(succ))
q.Enqueue(succ);
taboo.Add(succ);
WriteLine("No solution found");
Edit 2: The solution was using a HashSet. (see below)