@@ -45,10 +45,10 @@ boolean search(char[][] board, String word, int i, int j, int pos) {
4545 return false ;
4646 }
4747 visited [i ][j ] = true ;
48- if (search (board , word , i + 1 , j , pos + 1 )
49- || search (board , word , i - 1 , j , pos + 1 )
50- || search (board , word , i , j + 1 , pos + 1 )
51- || search (board , word , i , j - 1 , pos + 1 )) {
48+ if (search (board , word , i + 1 , j , pos + 1 )
49+ || search (board , word , i - 1 , j , pos + 1 )
50+ || search (board , word , i , j + 1 , pos + 1 )
51+ || search (board , word , i , j - 1 , pos + 1 )) {
5252 return true ;
5353 }
5454
@@ -58,39 +58,39 @@ boolean search(char[][] board, String word, int i, int j, int pos) {
5858
5959 }
6060
61- //I made it this time, completely by myself! Cheers! This let me completely understand backtracking!
61+ //I made it this time, completely by myself! Cheers! This let me completely understand backtracking!
6262 public boolean exist (char [][] board , String word ) {
6363 int m = board .length , n = board [0 ].length ;
64- for (int i = 0 ; i < m ; i ++){
65- for (int j = 0 ; j < n ; j ++){
64+ for (int i = 0 ; i < m ; i ++) {
65+ for (int j = 0 ; j < n ; j ++) {
6666 boolean [][] visited = new boolean [m ][n ];
67- if (dfs (board , visited , i , j , word , 0 )) {
67+ if (dfs (board , visited , i , j , word , 0 )) {
6868 return true ;
6969 }
7070 }
7171 }
7272 return false ;
7373 }
74-
75- final int [] dirs = new int []{0 ,1 , 0 , -1 ,0 };
76-
77- boolean dfs (char [][] board , boolean [][] visited , int row , int col , String word , int index ){
78- if (index >= word .length () || word .charAt (index ) != board [row ][col ]) {
74+
75+ final int [] dirs = new int []{0 , 1 , 0 , -1 , 0 };
76+
77+ boolean dfs (char [][] board , boolean [][] visited , int row , int col , String word , int index ) {
78+ if (index >= word .length () || word .charAt (index ) != board [row ][col ]) {
7979 return false ;
80- } else if (index == word .length ()- 1 && word .charAt (index ) == board [row ][col ]) {
80+ } else if (index == word .length () - 1 && word .charAt (index ) == board [row ][col ]) {
8181 visited [row ][col ] = true ;
8282 return true ;
8383 }
8484 visited [row ][col ] = true ;//set it to true for this case
8585 boolean result = false ;
86- for (int i = 0 ; i < 4 ; i ++){
87- int nextRow = row + dirs [i ];
88- int nextCol = col + dirs [i + 1 ];
89- if (nextRow < 0 || nextRow >= board .length || nextCol < 0 || nextCol >= board [0 ].length || visited [nextRow ][nextCol ]) {
86+ for (int i = 0 ; i < 4 ; i ++) {
87+ int nextRow = row + dirs [i ];
88+ int nextCol = col + dirs [i + 1 ];
89+ if (nextRow < 0 || nextRow >= board .length || nextCol < 0 || nextCol >= board [0 ].length || visited [nextRow ][nextCol ]) {
9090 continue ;
9191 }
92- result = dfs (board , visited , nextRow , nextCol , word , index + 1 );
93- if (result ) {
92+ result = dfs (board , visited , nextRow , nextCol , word , index + 1 );
93+ if (result ) {
9494 return result ;
9595 } else {
9696 visited [nextRow ][nextCol ] = false ;//set it back to false if this road doesn't work to allow it for other paths, this is backtracking!!!
@@ -99,7 +99,7 @@ boolean dfs(char[][] board, boolean[][] visited, int row, int col, String word,
9999 return result ;
100100 }
101101
102- public static void main (String ...strings ){
102+ public static void main (String ... strings ) {
103103 _79 test = new _79 ();
104104// char[][] board = new char[][]{
105105// {'A','B','C','E'},
@@ -109,16 +109,16 @@ public static void main(String...strings){
109109// String word = "ABCCED";
110110// String word = "SEE";
111111// String word = "ABCD";
112-
112+
113113// char[][] board = new char[][]{
114114// {'a','a'},
115115// };
116116// String word = "aaa";
117-
117+
118118 char [][] board = new char [][]{
119- {'A' ,'B' ,'C' ,'E' },
120- {'S' ,'F' ,'E' ,'S' },
121- {'A' ,'D' ,'E' ,'E' },
119+ {'A' , 'B' , 'C' , 'E' },
120+ {'S' , 'F' , 'E' , 'S' },
121+ {'A' , 'D' , 'E' , 'E' },
122122 };
123123 String word = "ABCEFSADEESE" ;
124124 System .out .println (test .exist (board , word ));
0 commit comments