6

I'm trying to come up with a good and fast heuristic for a clear-map pacman game.

My heuristic is trying to calculate the smallest distance possible that the pacman needs to travel to go to every points with food on the map. My current algorithm is basicly Prim's MST which gets me a O(n logn) running time, but doen't account for situations where the pacman needs to follow a edge to eat, and the return to the previous vertex...

Is there anything better?

Saying in another way: What is the minimum cost of connecting several dots without lifting my pen?

Thanks

mmcdole
  • 91,488
  • 60
  • 186
  • 222

1 Answers1

4

After running an all-pairs-shortest-path algorithm and identifying the vertices with food, this becomes the traveling salesman problem. You can't solve it efficiently, but you can construct arbitrarily good approximations to a solution in polynomial time. You'll probably want to use an approximation unless you can pre-compute everything. If you can pre-compute things (or otherwise guarantee that you have enough time to find an exact solution), then once you've got the all-pairs-shortest-paths, you can simply find the minimum total walk length over all possible permutations of the order in which you eat the food pieces. This brute-force method can probably be improved somewhat by watching out for when the shortest path between two food pieces crosses another food piece.

user57368
  • 5,675
  • 28
  • 39
  • And what would be the easiest way to approximate the solution to TSP? – Chetan Feb 03 '11 at 06:38
  • There's a mistake in my answer above: in order to have a bounded-error approximation, you need to change the constraints slightly. The all-pairs shortest path step guarantees that the distances are metric (at the expense of possibly having you revisit a point), which allows you to use Christofides's algorithm to get within 150% of the minimum. If you can further guarantee that the distances are Euclidean (ie. straight-line distances in normal geometry), then you can construct arbitrarily good approximations, but they are usually not worth the extra trouble. See the wikipedia article for more. – user57368 Feb 03 '11 at 08:54