简体   繁体   中英

Shortest path between two points in coordinates system

I have two points A and B. I want to find the shortest path from A to B, but there are N (up to 200) rectangles and the path cannot intersect any of those rectangles. Path and rectangles can intersect only at vertices of rectangles and at sides of rectangle. What is the length of the shortest path? Rectangles cannot intersect. They can share point or a side. So if there are two of them sharing a side then you can pass between them.

Usually the best algorithm for this kind of problems is A* using a simple heuristic like Manhattan Distance . But first you should find the illegal points. Illegal points are the ones that you can't enter them in this problem the points that are inside a rectangle are illegal (points that are located on the sides of the rectangles are legal since you can pass through them). After finding these points just implement an A* algorithm to find the shortest path between A and B .

Note that because there is no edge weight in this problem you can simply run a BFS to find the shortest path but it won't be as fast as A*.

If your search space is very big and you run out of memory using A*, you should consider using IDA* which consumes less memory but explores nodes more than once.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM