Skip to content
This repository has been archived by the owner on Nov 3, 2020. It is now read-only.

Commit

Permalink
Merge pull request #195 from Ishikagarg-ig/programming
Browse files Browse the repository at this point in the history
  • Loading branch information
Yashank18 authored Oct 8, 2020
2 parents 77bd803 + ddf2295 commit 657eb45
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions Coding/Java/backtracking_ratMaze.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@

import java.util.*;

class Rat{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();

while(t-- > 0){
int n = sc.nextInt();
int[][] a = new int[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
a[i][j]=sc.nextInt();
}
}

GFG g = new GFG();
ArrayList<String> res = g.printPath(a,n);
if(res.size()>0){
for(int i=0;i<res.size();i++){
System.out.print(res.get(i)+" ");
}
System.out.println();
}
else{
System.out.println(-1);
}
}
}
}

class GfG {
ArrayList<String> list;
public ArrayList<String> printPath(int[][] m, int n) {
boolean[][] vis=new boolean[n][n];

list = new ArrayList<>();
String path="";

ratMaze("",m,n,0,0,vis,path);

Collections.sort(list);
return list;
}

boolean isPosible(int[][]m ,int i, int j,int n,boolean[][] vis){
return i>=0 && j>=0 && i<n && j<n && m[i][j]==1 && vis[i][j]==false;
}

public void ratMaze(String direction,int[][] m,int n,int i,int j,boolean[][] vis,String path){
if(!isPosible(m,i,j,n,vis)) return;
if(i==n-1 && j==n-1){
path+=direction;
list.add(path);

path="";
return;
}

vis[i][j]=true;
path+=direction;
ratMaze("L",m,n,i,j-1,vis,path);
ratMaze("D",m,n,i+1,j,vis,path);
ratMaze("R",m,n,i,j+1,vis,path);
ratMaze("U",m,n,i-1,j,vis,path);

vis[i][j]=false;

return;

}
}

0 comments on commit 657eb45

Please sign in to comment.