From 77a9a42d8b5b65a414c3f1d527bc1d9d04e7e663 Mon Sep 17 00:00:00 2001 From: vamsee Date: Sat, 16 Oct 2021 15:00:41 +0530 Subject: Added DFS working code in java --- java/DFS.txt | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 java/DFS.txt diff --git a/java/DFS.txt b/java/DFS.txt new file mode 100644 index 0000000..800846c --- /dev/null +++ b/java/DFS.txt @@ -0,0 +1,80 @@ +package com.company; + +public class CG_pathfinder { + + static int paths=0; + private static final int[][] dir=new int[][]{ + {0,1}, //right + {1,0}, //down + {0,-1}, //left + {-1,0} //up + }; + + static void traverse(int x,int y,char[][] maze,boolean[][] visited){ + int curx; + int cury; + for(int i=0;i<4;i++){ + + curx=x; + cury=y; + curx+=dir[i][0]; + cury+=dir[i][1]; + if(curx<0 || cury<0 || curx> maze.length-1 || cury>maze.length-1){ + continue; //optional? //for square mazes + } + if(maze[curx][cury]=='$'){ + System.out.println("Path Found"); + paths++; + for(int k=0;k< maze.length;k++){ + for(int j=0;j