Reputation: 781
Problem:
Modify the A* algorithm we optimized for fewer turns. The algorithm will now find a path from (a,b) to ANY TILE ADJACENT TO (x,y), where (x+1,y) or (x-1,y) are favored when possible.
My attempted solution:
Visual Demonstration: (path from S to E where X is inaccessible)
......S .....S
. X . X
. => .
. .
E E.
However I'm not sure that my solution would work in some cases, ie:
......S .....S
. X . X
.X ??? X.
. .
E E..
Can anyone think of a solution to this problem?
Note: The A* algorithm is generic other than factoring in the number of direction changes to favor less turns in the resulting path.
Upvotes: 3
Views: 336
Reputation: 46399
Modify your heuristic function to give a slight boost to the points (x+1,y)
and (x-1,y)
. Then if you're 2 steps from finishing the path, ties will be broken in favor of those 2 points.
Upvotes: 0
Reputation: 178411
A* is actually an informed version of Dijkstra's algorithm, thus it is actually designed [with admissible heuristic function] to find shortest path to ALL vertices [from a single source].
You can define all tiles adjacent to (x,y)
as target vertices [A* can handle neatly multiple targets], and you will also need to modify the heuristic function, to give an admissible value to any target node. This can be done by simply modifying h'(tile) = max { h(tile) - 1 , 0}
- but in some cases you might have better way to do it.
After establishing the above, we can derive the following modification to the original A*:
d
.A*
with the current state, until the minimum
f(tile)
value for open nodes is strictly greater then d
. You are
guaranteed to find all tiles that are reachable from the source by a paths of length d
, and specifically - you will find all target tiles which have path from the source of length d
. [assuming admissible heuristic function].I hope that helps!
Upvotes: 2