-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.js
56 lines (55 loc) · 1.58 KB
/
solution.js
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
const filePath = require('path').join(__dirname, 'input');
const [n, ...arr] = require('fs')
.readFileSync(filePath)
.toString()
.trim()
.split('\n');
function solution (n, grid) {
const dp = Array.from({ length: n },
() => Array.from({ length: n },
() => new Array(3).fill(0))
);
const connectable = (y, x, d) => {
if (y < 0 || y >= n || x < 0 || x >= n) return false;
if (d === 0 || d === 2) {
return !grid[y][x];
} else {
return grid[y][x] === 0 && grid[y - 1][x] === 0 && grid[y][x - 1] === 0;
}
}
dp[0][1][0] = 1;
for (let i = 0; i < n; i ++) {
for (let j = 1; j < n; j ++) {
// 가로 -> 가로
if (connectable(i, j + 1, 0)) {
dp[i][j + 1][0] += dp[i][j][0];
}
// 가로 -> 대각선
if (connectable(i + 1, j + 1, 1)) {
dp[i + 1][j + 1][1] += dp[i][j][0];
}
// 대각선 -> 가로
if (connectable(i, j + 1, 0)) {
dp[i][j + 1][0] += dp[i][j][1];
}
// 대각선 -> 대각선
if (connectable(i + 1, j + 1, 1)) {
dp[i + 1][j + 1][1] += dp[i][j][1];
}
// 대각선 -> 세로
if (connectable(i + 1, j, 2)) {
dp[i + 1][j][2] += dp[i][j][1];
}
// 세로 -> 대각선
if (connectable(i + 1, j + 1, 1)) {
dp[i + 1][j + 1][1] += dp[i][j][2];
}
// 세로 -> 세로
if (connectable(i + 1, j, 2)) {
dp[i + 1][j][2] += dp[i][j][2];
}
}
}
return dp[n - 1][n - 1][0] + dp[n - 1][n - 1][1] + dp[n - 1][n - 1][2];
}
console.log(solution(+n, arr.map(r => r.split(' ').map(Number))));