Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

You preprocess the grid into connected components, marking every grid cell with the CC it is in. Then before pathfinding check that the start and end are in the same component. This is an O(1) check, albeit with a potential of O(n log n) space required for storing the array of CC lookups.


Whoops, make that an O(log n) check, technically speaking, as the index of which CC a cell belongs to may take O(log n) space worst-case.

Though you can treat it as effectively O(1) time and O(n) space.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: