Skip to content

Rat in a maze problem using backtracking - #278

Open
jasmine-k05 wants to merge 1 commit into
swapnanildutta:masterfrom
jasmine-k05:jasmine-k05-patch-1
Open

Rat in a maze problem using backtracking#278
jasmine-k05 wants to merge 1 commit into
swapnanildutta:masterfrom
jasmine-k05:jasmine-k05-patch-1

Conversation

@jasmine-k05

Copy link
Copy Markdown

/* C++ program for rat in a maze problem */

#include

using namespace std;

bool isSafe(int r,int c,int n,int **maze)

{

if( r < 0 || r >= n )

return false;

if( c < 0 || c >= n )

return false;

if(maze[r][c])

return true;

return false;

}

bool solvemaze(int ** maze,int i,int j,int ** soln,int n)

{

if(i == n-1 && j == n-1)

{

soln[i][j]=1;

return true;

}

if(isSafe(i,j,n,maze))

{

soln[i][j]=1;

if(solvemaze(maze,i+1,j,soln,n))

return true;

if(solvemaze(maze,i,j+1,soln,n))

return true;

soln[i][j]=0;

}

return false;

}

int main()

{

int n;

cin>>n;

int** maze = new int*[n];

for(int i = 0; i < n; ++i)

maze[i] = new int[n];

int** soln = new int*[n];

for(int i = 0; i < n; ++i)

soln[i] = new int[n];

for(int i=0;i<n;i++)

{

for(int j=0;j<n;j++)

{

cin>>maze[i][j];

soln[i][j]=0;

}

}

if(solvemaze(maze,0,0,soln,n))

{

for(int i=0;i<n;i++)

{

for(int j=0;j<n;j++)

cout<<soln[i][j]<<" ";

cout<<endl;

}

}

else

{

cout<<"-1";

}

}

Rat in a maze problem using backtracking

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @jasmine-k05 , thank you submitting a pull request!

@swapnanildutta

Copy link
Copy Markdown
Owner

Please add the entire questions on top as a comment.

@swapnanildutta swapnanildutta added help wanted Extra attention is needed incomplete invalid This doesn't seem right labels Oct 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

help wanted Extra attention is needed incomplete invalid This doesn't seem right

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants