Rat in a Maze Problem
PROBLEM :
Consider a rat placed at (0, 0) in a square matrix m[][] of order n and has to reach the destination at (n-1, n-1). Your task is to complete the function which returns a sorted array of strings denoting all the possible directions which the rat can take to reach the destination at (n-1, n-1). The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right).
For example
1 0 0 0
1 1 0 1
1 1 0 0
0 1 1 1
For the above matrix the rat can reach the destination at (3, 3) from (0, 0) by two paths ie DRDDRR and DDRDRR when printed in sorted order we get DDRDRR DRDDRR.
Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains two lines. The first line contains an integer n denoting the size of the square matrix. The next line contains n*n space separated values of the matrix m where 0's represents blocked paths and 1 represent valid paths.
Output:
For each test case output will be space separated sorted strings denoting all directions, which the rat could take to reach the destination.
Constraints:
1<=T<=10
2<=n<=10
0<=m[][]<=1
Example(To be used only for expected output):
Input:
2
4
1 0 0 0 1 1 0 1 0 1 0 0 0 1 1 1
4
1 0 0 0 1 1 0 1 1 1 0 0 0 1 1 1
Output:
DRDDRR
DDRDRR DRDDRR
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
bool is_safe( int m[MAX][MAX],int x,int y,int n)
{
if(((x>=0&&x<n)&&(y>=0&&y<n))&&m[x][y]==1)
return true;
return false;
}
void solve_maze(int m[MAX][MAX], int n,int i,int j,string str,vector<string> &path,int temp[MAX][MAX])
{
if(i==n-1&&j==n-1)
{
path.push_back(str);
return ;
}
temp[i][j]=1 ;
//going down
if(!temp[i+1][j])
if(is_safe(m,i+1,j,n))
{
temp[i+1][j]=1 ;
solve_maze(m,n,i+1,j,str+"D",path,temp);
temp[i+1][j]=0 ;
}
//going right
if(!temp[i][j+1])
if(is_safe(m,i,j+1,n))
{
temp[i][j+1]=1 ;
solve_maze(m,n,i,j+1,str+"R",path,temp);
temp[i][j+1]=0 ;
}
//going up
if(!temp[i-1][j])
if(is_safe(m,i-1,j,n))
{
temp[i-1][j]=1 ;
solve_maze(m,n,i-1,j,str+"U",path,temp);
temp[i-1][j]=0 ;
}
//going left
if(!temp[i][j-1])
if(is_safe(m,i,j-1,n))
{
temp[i][j-1]=1 ;
solve_maze(m,n,i,j-1,str+"L",path,temp);
temp[i][j-1]=0 ;
}
}
vector<string> printPath(int m[MAX][MAX], int n)
{
vector<string> path;
string str="";
int temp[MAX][MAX]={0} ;
solve_maze(m,n,0,0,str,path,temp);
sort(path.begin(),path.end());
return path;
}
--------------------------------------------------------------------------------
Consider a rat placed at (0, 0) in a square matrix m[][] of order n and has to reach the destination at (n-1, n-1). Your task is to complete the function which returns a sorted array of strings denoting all the possible directions which the rat can take to reach the destination at (n-1, n-1). The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right).
For example
1 0 0 0
1 1 0 1
1 1 0 0
0 1 1 1
For the above matrix the rat can reach the destination at (3, 3) from (0, 0) by two paths ie DRDDRR and DDRDRR when printed in sorted order we get DDRDRR DRDDRR.
Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains two lines. The first line contains an integer n denoting the size of the square matrix. The next line contains n*n space separated values of the matrix m where 0's represents blocked paths and 1 represent valid paths.
Output:
For each test case output will be space separated sorted strings denoting all directions, which the rat could take to reach the destination.
Constraints:
1<=T<=10
2<=n<=10
0<=m[][]<=1
Example(To be used only for expected output):
Input:
2
4
1 0 0 0 1 1 0 1 0 1 0 0 0 1 1 1
4
1 0 0 0 1 1 0 1 1 1 0 0 0 1 1 1
Output:
DRDDRR
DDRDRR DRDDRR
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
bool is_safe( int m[MAX][MAX],int x,int y,int n)
{
if(((x>=0&&x<n)&&(y>=0&&y<n))&&m[x][y]==1)
return true;
return false;
}
void solve_maze(int m[MAX][MAX], int n,int i,int j,string str,vector<string> &path,int temp[MAX][MAX])
{
if(i==n-1&&j==n-1)
{
path.push_back(str);
return ;
}
temp[i][j]=1 ;
//going down
if(!temp[i+1][j])
if(is_safe(m,i+1,j,n))
{
temp[i+1][j]=1 ;
solve_maze(m,n,i+1,j,str+"D",path,temp);
temp[i+1][j]=0 ;
}
//going right
if(!temp[i][j+1])
if(is_safe(m,i,j+1,n))
{
temp[i][j+1]=1 ;
solve_maze(m,n,i,j+1,str+"R",path,temp);
temp[i][j+1]=0 ;
}
//going up
if(!temp[i-1][j])
if(is_safe(m,i-1,j,n))
{
temp[i-1][j]=1 ;
solve_maze(m,n,i-1,j,str+"U",path,temp);
temp[i-1][j]=0 ;
}
//going left
if(!temp[i][j-1])
if(is_safe(m,i,j-1,n))
{
temp[i][j-1]=1 ;
solve_maze(m,n,i,j-1,str+"L",path,temp);
temp[i][j-1]=0 ;
}
}
vector<string> printPath(int m[MAX][MAX], int n)
{
vector<string> path;
string str="";
int temp[MAX][MAX]={0} ;
solve_maze(m,n,0,0,str,path,temp);
sort(path.begin(),path.end());
return path;
}
--------------------------------------------------------------------------------
Comments
Post a Comment