I required some help in solving this question by recursion.
Question:
A lazy tourist wants to visit as many interesting locations in a city as possible without going one step further than necessary. Starting from his hotel, located in the north-west corner of city, he intends to take a walk to the south-east corner of the city and then walk back. When walking to the south-east corner, he will only walk east or south, and when walking back to the north-west corner, he will only walk north or west. After studying the city map he realizes that the task is not so simple because some areas are blocked. Therefore he has kindly asked you to write a program to solve his problem.
Given the city map (a 2D grid) where walkable areas are marked by ".", interesting locations are marked by "*", and blocked areas are marked by "#", determine the maximum number of interesting locations he can visit. Locations visited twice are only counted once.
*........
.....**#.
..**...#*
..####*#.
.*.#*.*#.
...#**...
*........
Ans : 7.
My initial thoughts were to solve this problem in two parts.
1) When the tourist starts from top-left corner.
2) When the tourist starts from down-right corner.
For the answer add them both parts, and that would be it.
Here's what the method looks like for the first part.
i and j were initially 0, 0.
path(int i, int j){
if(i == H && j == W){
return (strV[i][j] == '*')?1:0;
}
if(i >H || j >W || i<0 || j<0){
return 0;
}
if(visited[i][j] == 1){
//cout<<"vis";
return 0;
}
if(strV[i][j] == '#' ){
return 0;
}
visited[i][j] =1;
cout<<i<<" "<<j<<"\n";
// as i can go only down and right ,for the first part.
// south and east.
return max(path(i+1, j), path (i, j+1) ) + (strV[i][j] == '*')?1 :0;
}
Similarly I tried calculating for the second part. As I've maintained the visited array, there is no possibility of revisiting the same point. Hope that should satisfy the condition as they mention in the question. I'm not sure if this will solve the question, it's giving incorrect answer.
Here's the question link: http://www.spoj.com/problems/TOURIST/
Thanks for your time.
Your approach has a few flaws. Some that make it give the wrong solution and some that are just lead to an inefficient search.
The flaws that lead to a wrong solution are:
The flaw that leads to an inefficient search is not simplifying the search domain before searching for the best path. By creating a graph that only contains the interesting locations as nodes and connections between them when there is a valid (non-blocked) path between them you can decrease your search space considerably (depending on how sparse the interesting locations and abundant the blocks are)
Keeping as close to your attempt as possible my suggestions are:
Hope this is helpful
Here's a solution in JavaScript. It recursively tries all valid directions SE and NW, and saves its result if it's greater than the current max result.
EDIT Here's the jsfiddle http://jsfiddle.net/reeyws86/ You may want to add logic to avoid corner cases like when the grid is empty or only has 1 square.
A few observations will help you solve this problem more easily:
Code: