-
Notifications
You must be signed in to change notification settings - Fork 0
/
B2186.java
100 lines (80 loc) · 2.46 KB
/
B2186.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import java.util.*;
import java.io.*;
class B2186 {
static int[] dx = {-1, 0, 1, 0};
static int[] dy = {0, -1, 0, 1};
static int N, M, K;
static char[][] arr;
static String end;
static Integer[][][] dp;
static int result;
public static void main (String[] args) throws IOException {
input();
solution();
output();
}
public static void solution() {
initDP();
fillDP();
}
public static void fillDP() {
int len = end.length() - 1;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
result += dfs(len, i, j);
}
}
}
public static int dfs(int len, int i, int j) {
if (dp[len][i][j] == null) {
dp[len][i][j] = 0;
if (arr[i][j] == end.charAt(len)) {
for (int k = 1; k <= K; k++) {
for (int d = 0; d < dx.length; d++) {
int nx = i + dx[d] * k;
int ny = j + dy[d] * k;
if (nx < 0 || ny < 0 || nx >= N || ny >= M) continue;
dp[len][i][j] += dfs(len - 1, nx, ny);
}
}
}
}
return dp[len][i][j];
}
public static void initDP() {
char s = end.charAt(0);
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (arr[i][j] == s) {
dp[0][i][j] = 1;
}
else {
dp[0][i][j] = 0;
}
}
}
}
public static void output() throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
bw.write(result+"");
bw.flush();
bw.close();
}
public static void input() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
arr = new char[N][M];
for (int i = 0; i < N; i++) {
String line = br.readLine();
for (int j = 0; j < M; j++) {
arr[i][j] = line.charAt(j);
}
}
end = br.readLine();
dp = new Integer[end.length()][N][M];
br.close();
}
}