forked from iver3on/OjTask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main2.java
73 lines (67 loc) · 1.94 KB
/
Main2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.util.Scanner;
public class Main2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
int n = Integer.parseInt(s.split(" ")[0]);
int m = Integer.parseInt(s.split(" ")[1]);
String word = sc.nextLine();
char[][] arr = new char[n][m];
for (int i = 0; i < n; i++) {
String line = sc.nextLine();
for (int j = 0; j < m; j++) {
arr[i][j] = line.charAt(j);
}
}
if (isLine(arr, word)) {
System.out.println("YES");
} else
System.out.println("NO");
}
private static boolean isLine(char[][] arr, String word) {
int row = arr.length;
int col = arr[0].length;
boolean[][] flag = new boolean[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
flag[i][j] = true;
}
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (arr[i][j] == word.charAt(0)) {
flag[i][j] = false;
if (isExits(arr, i, j, word.substring(1), flag)) {
return true;
}
flag[i][j] = false;
}
}
}
return false;
}
private static boolean isExits(char[][] board, int i, int j, String w, boolean[][] flag) {
if (w.length() == 0)
return true;
char c = w.charAt(0);
if (i + 1 < board.length && board[i + 1][j] == c && isExits(board, i + 1, j, w.substring(1), flag)
&& flag[i + 1][j]) {
flag[i + 1][j] = false;
return true;
}
if (i >= 1 && board[i - 1][j] == c && isExits(board, i - 1, j, w.substring(1), flag) && flag[i - 1][j]) {
flag[i - 1][j] = false;
return true;
}
if (j + 1 < board[0].length && board[i][j + 1] == c && isExits(board, i, j + 1, w.substring(1), flag)
&& flag[i][j + 1]) {
flag[i][j + 1] = false;
return true;
}
if (j >= 1 && board[i][j - 1] == c && isExits(board, i, j - 1, w.substring(1), flag) && flag[i][j - 1]) {
flag[i][j - 1] = false;
return true;
}
return false;
}
}