Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rat in a maze problem using backtracking #278

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jasmine-k05
Copy link

/* 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
Copy link

@github-actions github-actions bot left a comment

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
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