File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ // BFS로 변환해서 다시 풀어볼 것
3
+ char [][] board ;
4
+ boolean [][] visited ;
5
+ int [] moveX = {-1 , 1 , 0 , 0 };
6
+ int [] moveY = {0 , 0 , -1 , 1 };
7
+ int row , col ;
8
+
9
+ public boolean exist (char [][] board , String word ) {
10
+ this .board = board ;
11
+ this .row = board .length ;
12
+ this .col = board [0 ].length ;
13
+ this .visited = new boolean [row ][col ];
14
+
15
+ for (int y = 0 ; y < row ; y ++) {
16
+ for (int x = 0 ; x < col ; x ++) {
17
+ if (this .hasWord (y , x , word , 0 )) {
18
+ return true ;
19
+ }
20
+ }
21
+ }
22
+
23
+ return false ;
24
+ }
25
+
26
+ private boolean hasWord (int y , int x , String word , int index ) {
27
+ if (index >= word .length ()) {
28
+ return true ;
29
+ }
30
+
31
+ if (x < 0 || x >= col || y < 0 || y >= row || visited [y ][x ] || board [y ][x ] != word .charAt (index )) {
32
+ return false ;
33
+ }
34
+
35
+ this .visited [y ][x ] = true ;
36
+ for (int i = 0 ; i < 4 ; i ++) {
37
+ if (this .hasWord (y + moveY [i ], x + moveX [i ], word , index + 1 )) {
38
+ return true ;
39
+ }
40
+ }
41
+
42
+ this .visited [y ][x ] = false ;
43
+ return false ;
44
+ }
45
+ }
You can’t perform that action at this time.
0 commit comments