Skip to content

Commit 95f4fc6

Browse files
authored
[20250404] BOJ / G4 / 파이프 옮기기2 / 신희을
1 parent d359503 commit 95f4fc6

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
class Main {
6+
7+
8+
static int[] di = {0,1,1};
9+
static int[] dj = {1,0,1};
10+
static boolean[][] map;
11+
static int N;
12+
public static void main(String args[]) throws Exception {
13+
14+
N = read();
15+
16+
map = new boolean[N][N];
17+
18+
for(int i = 0; i < N; i++) {
19+
for(int j = 0; j < N; j++) map[i][j] = read() == 1;
20+
}
21+
22+
long[][][] dp = new long[N][N][3];
23+
24+
dp[0][1][0] = 1;
25+
26+
for(int i = 0; i < N; i++) {
27+
for(int j = 0; j < N; j++) {
28+
int ni = i + 1;
29+
int nj = j + 1;
30+
// 가로로 놓여있을때
31+
if(check(i, nj, true)) dp[i][nj][0] += dp[i][j][0];
32+
if(check(ni, nj, false)) dp[ni][nj][2] += dp[i][j][0];
33+
34+
// 세로로 놓여있을때
35+
if(check(ni, j, true)) dp[ni][j][1] += dp[i][j][1];
36+
if(check(ni, nj, false)) dp[ni][nj][2] += dp[i][j][1];
37+
38+
// 대각선으로 놓여있을 때
39+
if(check(ni, j, true)) dp[ni][j][1] += dp[i][j][2];
40+
if(check(i, nj, true)) dp[i][nj][0] += dp[i][j][2];
41+
if(check(ni, nj, false)) dp[ni][nj][2] += dp[i][j][2];
42+
43+
}
44+
}
45+
46+
// for(int i = 0; i < N; i++) {
47+
// for(int j = 0; j < N; j++) {
48+
// System.out.print(dp[i][j][0] + "" + dp[i][j][1] + "" + dp[i][j][2] + " ");
49+
// }
50+
// System.out.println();
51+
// }
52+
//
53+
System.out.println(dp[N-1][N-1][0] + dp[N-1][N-1][1] + dp[N-1][N-1][2]);
54+
}
55+
56+
public static boolean check(int i, int j, boolean b) {
57+
if(b)return i >= 0 && j >= 0 && i < N && j < N && !map[i][j];
58+
return i >= 0 && j >= 0 && i < N && j < N && !map[i][j] && !map[i-1][j] && !map[i][j-1];
59+
}
60+
61+
62+
private static int read() throws Exception {
63+
int c;
64+
int n = 0;
65+
boolean negative = false;
66+
67+
while ((c = System.in.read()) <= 32) {
68+
if (c == -1) return -1;
69+
}
70+
71+
if (c == '-') {
72+
negative = true;
73+
c = System.in.read();
74+
}
75+
76+
do {
77+
n = n * 10 + (c - '0');
78+
c = System.in.read();
79+
} while (c > 32);
80+
81+
return negative ? -n : n;
82+
}
83+
84+
85+
}
86+
```

0 commit comments

Comments
 (0)