-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaekjoon_15683.js
88 lines (72 loc) · 2.12 KB
/
baekjoon_15683.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
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
const input = require("fs").readFileSync('/dev/stdin')
.toString()
.trim()
.split('\n');
const [size, ...field_input] = input;
const [N, M] = size.split(' ');
const field = Array.from({ length: N * 1 }, () => Array(M * 1));
const visited = Array.from({ length: N * 1 }, () => Array(M * 1).fill(false));
const cctvs = [];
const ways = [
null,
[
[[0, +1]],
[[+1, 0]],
[[0, -1]],
[[-1, 0]],
],
[
[[0, +1], [0, -1]],
[[+1, 0], [-1, 0]],
],
[
[[-1, 0], [0, +1]],
[[0, +1], [+1, 0]],
[[+1, 0], [0, -1]],
[[0, -1], [-1, 0]],
],
[
[[0, -1], [-1, 0], [0, +1]],
[[-1, 0], [0, +1], [+1, 0]],
[[0, +1], [+1, 0], [0, -1]],
[[+1, 0], [0, -1], [-1, 0]],
],
[[[0, +1], [+1, 0], [0, -1], [-1, 0]],],
];
let blindSpot = 0;
field_input.forEach((row, y) => {
row.trim().split(' ').forEach((item, x) => {
const _item = item * 1;
if(!_item) blindSpot ++;
else if(_item < 6) cctvs.push([y, x]);
field[y][x] = _item;
});
});
const fillField = ([y, x], [wy, wx]) => {
let ny = y, nx = x;
const fill_arr = [];
while(true) {
ny += wy;
nx += wx;
if(ny < 0 || nx < 0 || ny >= N || nx >= M || field[ny][nx] === 6) break;
if(field[ny][nx] || visited[ny][nx]) continue;
fill_arr.push([ny, nx]);
visited[ny][nx] = true;
}
return fill_arr;
}
const dfs = (idx, view_cnt) => {
if(idx === cctvs.length) return view_cnt;
let max_cnt = 0;
const [y, x] = cctvs[idx];
const target = ways[field[y][x]];
target.forEach((way, way_idx) => {
const fill_pos = way.reduce((acc, curr) => [...acc, ...fillField([y, x], curr)], []);
if(!fill_pos.length && way_idx !== target.length - 1) return;
max_cnt = Math.max(max_cnt, dfs(idx + 1, fill_pos.length + view_cnt));
fill_pos.forEach(([fy, fx]) => visited[fy][fx] = false);
});
return max_cnt;
}
if(cctvs.length) console.log(blindSpot - dfs(0, 0));
else console.log(blindSpot);