Skip to content

Commit 3390769

Browse files
committed
[20251211] BOJ / G4 / 알고리즘 수업 - 행렬 경로 문제 4 / 김민진
1 parent d3fa6ac commit 3390769

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
```java
2+
import java.io.*;
3+
import java.util.Arrays;
4+
import java.util.StringTokenizer;
5+
6+
public class BJ_24427_알고리즘_수업_행렬_경로_문제_4 {
7+
8+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
10+
private static StringTokenizer st;
11+
12+
private static int N, P;
13+
private static int[][] matrix;
14+
private static int[][][] dp;
15+
private static boolean[][] target;
16+
17+
public static void main(String[] args) throws IOException {
18+
init();
19+
sol();
20+
}
21+
22+
private static void init() throws IOException {
23+
N = Integer.parseInt(br.readLine());
24+
25+
matrix = new int[N + 1][N + 1];
26+
for (int i = 1; i <= N; i++) {
27+
st = new StringTokenizer(br.readLine());
28+
for (int j = 1; j <= N; j++) {
29+
matrix[i][j] = Integer.parseInt(st.nextToken());
30+
}
31+
}
32+
33+
P = Integer.parseInt(br.readLine());
34+
target = new boolean[N + 1][N + 1];
35+
while (P-- > 0) {
36+
st = new StringTokenizer(br.readLine());
37+
38+
int x = Integer.parseInt(st.nextToken());
39+
int y = Integer.parseInt(st.nextToken());
40+
41+
target[x][y] = true;
42+
}
43+
44+
dp = new int[N + 1][N + 1][2];
45+
for (int i = 0; i <= N; i++) {
46+
for (int j = 0; j <= N; j++) {
47+
Arrays.fill(dp[i][j], -987654321);
48+
}
49+
}
50+
dp[1][1][target[1][1] ? 0 : 1] = matrix[1][1];
51+
}
52+
53+
private static void sol() throws IOException {
54+
for (int i = 1; i <= N; i++) {
55+
for (int j = 1; j <= N; j++) {
56+
if (i == 1 && j == 1) continue;
57+
58+
if (target[i][j]) {
59+
dp[i][j][0] = matrix[i][j] + Math.max(
60+
Math.max(dp[i - 1][j][1], dp[i][j - 1][1]),
61+
Math.max(dp[i - 1][j][0], dp[i][j - 1][0])
62+
);
63+
} else {
64+
dp[i][j][0] = matrix[i][j] + Math.max(dp[i - 1][j][0], dp[i][j - 1][0]);
65+
dp[i][j][1] = matrix[i][j] + Math.max(dp[i - 1][j][1], dp[i][j - 1][1]);
66+
}
67+
}
68+
}
69+
70+
bw.write(dp[N][N][0] + "");
71+
bw.flush();
72+
bw.close();
73+
br.close();
74+
}
75+
76+
}
77+
```

0 commit comments

Comments
 (0)