| Flood fill is the hard part - A* is basicaly flood fill too, just with heuristic and quick stop condition. Preprocessing is cheating :). If you preprocess and floodfill the map anyway, you can go one step further, do Dijkstra, save the results using a trick, and you don't need run-time pathfinding at all. And you can store full paths from each cell to each cell using just O(N * N) memory, no matter the path lengths - that's a neat trick BTW: you store for each cell pair (P,Q) what is the next cell you should go from P if you want to eventually get to Q. that allows yu to reconstruct full path between each pair of cells quickly. EDIT to clarify - cheating is good, it's just I don't think O(N log N) is good tradeoff for the ability to return quicker, if you can do away with the whole pathfinding for O(N * N). EDIT 2: why O(N log N)? It seems you could write the resulting component for each cell, so worst case O(N) space? |
In practice you can treat it as O(1) time and O(n) space, though.
Also, that preprocessing with result saving is substantially slower than the connectivity alone. The connectivity alone is O(n) / O(n log n) preprocessing time - you check each cell a maximum of 5 / 9 times (depending on 4 or 8 way connectivity).
Also, that preprocessing with result saving fails miserably when you do updates to the graph.