This is my code .
This code is printing all the possible solutions . How to stop this code from printing all the solutions .
import java.util.*;
public class Main {
static boolean exec = false ;
public static void main(String args[]) {
// Your Code Here
Scanner scan = new Scanner(System.in) ;
int row = scan.nextInt() ;
int col = scan.nextInt() ;
char[][] maze = new char[row][col] ;
for(int i = 0 ; i < row ; i ++){
for(int j = 0 ; j < col ; j ++){
maze[i][j] = scan.next().charAt(0) ;
}
}
int[][] sol = new int[row][col] ;
ratMaze(maze , 0 , 0 , sol) ;
if(!exec){
System.out.println(-1) ;
}
}
public static void ratMaze(char[][] maze , int row , int col , int[][] sol){
if(row == maze.length-1 && col == maze[0].length-1){
exec = true ;
sol[row][col] = 1 ;
for(int i = 0 ; i < sol.length ; i++){
for(int j = 0 ; j < sol[0].length ; j ++){
System.out.print(sol[i][j]+" ") ;
}
System.out.println() ;
}
return ;
}
if(row == maze.length || col == maze[0].length || maze[row][col] == 'X' || sol[row][col] == 1){
return ;
}
sol[row][col] = 1 ;
ratMaze(maze , row , col+1 , sol) ;
ratMaze(maze , row+1 , col , sol) ;
sol[row][col] = 0 ;
}
}