Recursive backtracking maze solver

Hello! I'm trying to write a program which finds if there is a path in a maze which is determinated by a 4x4 matrix. If the Aij element of the matrix = # then it is considered a wall.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  #include <iostream>

using namespace std;

bool isSafe(char maze[][4],int x,int y)
{
    if (maze[x][y]!='#' && x>=0 && y>=0 && x<4 && y<4)
    {
        return true;
    }
    return false;
}

bool isSolve(char maze[][4],int x,int y)
{
    if (x==3 && y==3)
    {
        return true;
    }
    maze [x][y]='#';

        if (isSafe(maze,x+1,y)==true)// SOUTH
        {
            isSolve(maze,x+1,y);
        }
        if (isSafe(maze,x,y-1)==true)// WEST
        {
            isSolve(maze,x,y-1);
        }
        if (isSafe(maze,x-1,y)==true)// NORTH
        {
            isSolve(maze,x-1,y);
        }
        if (isSafe(maze,x,y+1)==true)// EAST
        {
            isSolve(maze,x,y+1);
        }


    return false;

}

int main()
{
    char arr[4][4]={
    {'0','#','0','0'},
    {'0','0','0','#'},
    {'0','#','#','0'},
    {'0','0','0','0'}
    };

    cout<<isSolve(arr,0,0);

    return 0;
}


The path should start at (0,0) and end at (3,3). The example maze in my main should return value of 1, since there is a path, but it doesnt. Any advice?

EDIT: Ok guys, i inserted a little cout<<x<<y line in the beginning of my recursive function which should give me the current coordinates of every step. Well apparently my function behaves just as it should be and in my givem matrix it goes 0,0->1,0->2,0->3,0->3,1->3,2 ->3,3 but then instead of returning true, it starts going the other main branch of my maze (1,1 1,2 and so on).
EDIT2: Apparantly nothing in my if(x==3 && y==3) is executed so there goes why it doesnt return true as soon as it gets to 3,3. My question is why tho? and How can i make it work
Last edited on
isSolve correctly marks the current square when you enter it, but you don't clear it when you leave. Set it back to zero upon exit. Also you might want to set it to something other than '#' so you can see the path easier when it's found. If you do that, be sure to change isSafe to account for the other character.
Uhm.. I leave it marked # on purpose, so my recursion knows it shouldnt ever move forward to a cell that has already been visited. (Trying to avoid infinite cycle). (Moreover the task itselfs says that everytime my "knight" moves into a new cell the ceiling behind him crushes and the previous cell becomes a wall).
Uhm.. I leave it marked # on purpose, so my recursion knows it shouldnt ever move forward to a cell that has already been visited.


When you return from a recursive call you're returning to a previous state. Cells marked in the recursive calls you're returning from should be unmarked.

You might have a look at this thread:
http://www.cplusplus.com/forum/beginner/179261/
Topic archived. No new replies allowed.