- There are some cells which are must visit, marked in grey colour.
- Any cell can be visited any number of times(even the start, finish and must visit points)
for ex- █=Black, ░=white, G=grey, R=Red, O=orange ██████ █████ ██G███ █░GG█ MAZE1 => █O░GR█ MAZE2 => █O███ ██G███ █░░R█ ██████ █████
Here in this case ans will be
MAZE1 => M[2][1] => [2][2] => [1][2] => [2][2] => [3][2] => [2][2] => [2][3] => [2][4] = 7 MAZE2 => M[1][1] => [1][2] => [2][2] => [3][2] => [3][3] => [3][2] => [2][2] = 6
As you can see, the nodes appear multiple times First i thought of using recursion technique (backtracking) but couldn’t come to an algorithm. and So i thought of using this way.
- I will keep track of all the coordinates of must visit points, start and end points
- Find the distance between each node(like in selection sort we compare each and every term, just like that, we get the minimum distance between each node (using BFS))
- Then apply some minimum distance algorithm. I thought of TSP but it says nodes must be visited exactly once.Here it can be multiple times. I found chinese postman problem, but don’t know if it can be applied here. Floyd warshall algorithm is there but it doesn’t include every point
How should i proceed, any idea?
Asked By : zero infinity
Answered By : David Richerby
Take the northern-most of the cities and make it be the start square (if there are multiple cities equally far north, choose any one of them. Make every other city a grey square. Place the end square one square to the north of the start square and colour black the squares north, east and west of the end square. Make every other square white.
Because of the black walls, any route from the start square to the end square must revisit the start square immediately before going to the end. The end square and its black walls are outside the convex hull of the original cities, so no optimal tour of the original cities would have gone through those squares. Therefore, a shortest route from the start square to the end square consists of a solution to the rectilinear TSP problem (a shortest tour from the start city, through all the other cities and back to the start city), followed by the one-square move from the start square to the end square. This completes the reduction.
Best Answer from StackOverflow
Question Source : http://cs.stackexchange.com/questions/29312