Why my output is not correct
package Questions; import java.util.Scanner; public class RatInMaze { static int n,m; public static void main(String[] args) { // TODO Auto-generated method stub Scanner obj=new Scanner(System.in); n=obj.nextInt(); m=obj.nextInt(); String[][] board=new String[n][m]; String[][] sol=new String[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { sol[i][j]=“0”; } } for(int i=0;i<n;i++)// input taken { for(int j=0;j<m;j++) { board[i][j]=obj.next(); } } solveMaze(board,0,0,sol); } public static boolean solveMaze(String[][]board,int row,int col,String[][] sol) { if(col<0||col>=m||row<0||row>=n) return false; if(row==n-1&&col==m-1) { sol[row][col]=“1”; print(sol,row,col); return true; } if(board[row][col]==“X”||sol[row][col]==“1”) { return false; } sol[row][col]=“1”; if(solveMaze(board,row,col+1,sol)) { sol[row][col]=“0”; return true; } if(solveMaze(board,row,col-1,sol)) { sol[row][col]=“0”; return true; } if(solveMaze(board,row-1,col,sol)) { sol[row][col]=“0”; return true; } if(solveMaze(board,row+1,col,sol)) { sol[row][col]=“0”; return true; //if moving right side does not help move down } sol[row][col]=“0”; return false; } public static void print(String[][] sol,int row,int col) { if(sol[row][col]==“1”) { for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { System.out.print(" “+sol[i][j]+” “); } System.out.println(); } } else System.out.println(”-1"); } }
package Questions;
import java.util.Scanner;
public class RatInMaze {
static int n,m;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner obj=new Scanner(System.in);
n=obj.nextInt();
m=obj.nextInt();
String[][] board=new String[n][m];
String[][] sol=new String[n][m];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
sol[i][j]=“0”;
}
}
for(int i=0;i<n;i++)// input taken
{
for(int j=0;j<m;j++)
{
board[i][j]=obj.next();
}
}
solveMaze(board,0,0,sol);
}
public static boolean solveMaze(String[][]board,int row,int col,String[][] sol)
{
if(col<0||col>=m||row<0||row>=n)
return false;
if(row==n-1&&col==m-1)
{
sol[row][col]="1";
print(sol,row,col);
return true;
}
if(board[row][col]=="X"||sol[row][col]=="1")
{
return false;
}
sol[row][col]="1";
if(solveMaze(board,row,col+1,sol))
{
sol[row][col]="0";
return true;
}
if(solveMaze(board,row,col-1,sol))
{
sol[row][col]="0";
return true;
}
if(solveMaze(board,row-1,col,sol))
{
sol[row][col]="0";
return true;
}
if(solveMaze(board,row+1,col,sol))
{
sol[row][col]="0";
return true; //if moving right side does not help move down
}
sol[row][col]="0";
return false;
}
public static void print(String[][] sol,int row,int col)
{
if(sol[row][col]=="1")
{
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(" "+sol[i][j]+" ");
}
System.out.println();
}
}
else
System.out.println("-1");
}
}
see this is a ques of backtracking…when we need to print paths we generally keep the function type as void…so instead of boolean make your function type as void…
in the base case just print your board
also keep a boolean matrix visited of the same dimensions so that from a particular cell you dont call on a cell
which is already a part of your path…
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.