-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathb15684.js
More file actions
77 lines (67 loc) · 1.71 KB
/
b15684.js
File metadata and controls
77 lines (67 loc) · 1.71 KB
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
var fs = require("fs");
var input = fs.readFileSync("/dev/stdin").toString().trim().split("\n");
const [N, M, H] = input[0].split(" ").map(Number);
const horztls = input.slice(1).map((r) => r.split(" ").map(Number));
const board = Array(H)
.fill(0)
.map(() => Array(N).fill(0));
const newHorztls = [];
let min = -1;
for (const hls of horztls) {
const [a, b] = hls.map((n) => n - 1);
board[a][b] = 1;
}
for (let j = 0; j < H; j++) {
for (let i = 0; i < N - 1; i++) {
if (board[j][i] === 0) {
newHorztls.push([j, i]);
}
}
}
recursion(0, 0);
console.log(min);
function recursion(addCnt, n) {
if (addCnt > 3) {
return;
}
if (check(board)) {
min = min > addCnt || min === -1 ? addCnt : min;
}
if (n >= newHorztls.length) {
return;
}
// if 추가한 가로선과 함께 i -> i 사다리가 만들어지면, 최소값 업데이트, return
// 현재 가로선을 추가
for (let i = n; i < newHorztls.length; i++) {
const a = newHorztls[i][0];
const b = newHorztls[i][1];
const isNextLine = board[a][b + 1] === 1;
// const isLeftLine = board[a][b - 1] === 1;
const isNoLine = board[a][b] === 0;
if (isNoLine && !isNextLine) {
board[a][b] = 1;
// 추가 후 다음 가로선 재귀 시도
recursion(addCnt + 1, i + 1);
// 가로선 추가한것 삭제
board[a][b] = 0;
}
}
}
function check(board) {
// start
for (let i = 0; i < board[0].length; i++) {
let col = i;
// nth row
for (let j = 0; j < board.length; j++) {
if (col > 0 && board[j][col - 1]) {
col = col - 1;
} else if (board[j][col]) {
col = col + 1;
}
}
if (col !== i) {
return false;
}
}
return true;
}