i have written a code,but i am not able to think as to how to incorporate up and left movements of the rat. Below is the code I have written.
Rat chases its cheese problem. Module:Challenges:Linked List
Plz send your code by saving on ide only.
You need to maintain a visited array, bool visited[1001][1001]. make it true, and then check the conditions before traversing any path,
if(visited[i][j+1]==false && solution[i+1][j]==0)
{
downSuccess=ratmaze(maze,solution,i+1,j,N,M);
}
if(visited[i][j+1]==false && solution[i][j+1]==0)
{
rightSuccess=ratmaze(maze,solution,i,j+1,N,M);
}
Try to modify your code with this approach.
The code is working fine with the approach that i have used. I want to know how can I incorporate the left and up movements of rat.
Use these kind of statements in your recursive function, take the boolean variables, topSuccess and leftSuccess and then -
if(solution[i-1][j]==0)
{
topSuccess=ratmaze(maze,solution,i-1,j,N,M);
}
if(solution[i][j-1]==0)
{
leftSuccess=ratmaze(maze,solution,i,j-1,N,M);
}