Skip to content

Commit 760153e

Browse files
Merge pull request #740 from eric-hjh/main
[황장현] 121차 라이브 코테 제출
2 parents d3fdea4 + c488f60 commit 760153e

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const input = require('fs')
2+
.readFileSync(process.platform === 'linux' ? '/dev/stdin' : './input.txt')
3+
.toString()
4+
.trim()
5+
.split('\n');
6+
7+
const [N, M, K] = input[0].split(' ').map(Number);
8+
const jiminBoard = input.slice(1).map((el) => el.split(''));
9+
10+
const startBlack = Array.from({ length: N + 1 }, () => Array(M + 1).fill(0));
11+
const startWhite = Array.from({ length: N + 1 }, () => Array(M + 1).fill(0));
12+
13+
function calculateSum(board, start) {
14+
const height = jiminBoard.length;
15+
const width = jiminBoard[0].length;
16+
17+
for (let i = 0; i < height; i++) {
18+
for (let j = 0; j < width; j++) {
19+
if ((i + j) % 2 === 0) {
20+
board[i + 1][j + 1] = jiminBoard[i][j] === start ? 1 : 0;
21+
} else {
22+
board[i + 1][j + 1] = jiminBoard[i][j] === start ? 0 : 1;
23+
}
24+
}
25+
}
26+
27+
for (let i = 1; i <= height; i++) {
28+
for (let j = 1; j <= width; j++) {
29+
board[i][j] += board[i][j - 1] + board[i - 1][j] - board[i - 1][j - 1];
30+
}
31+
}
32+
}
33+
34+
calculateSum(startBlack, 'B');
35+
calculateSum(startWhite, 'W');
36+
37+
let minChanges = Infinity;
38+
for (let i = K; i <= N; i++) {
39+
for (let j = K; j <= M; j++) {
40+
const blackSum =
41+
startBlack[i][j] -
42+
startBlack[i - K][j] -
43+
startBlack[i][j - K] +
44+
startBlack[i - K][j - K];
45+
const whiteSum =
46+
startWhite[i][j] -
47+
startWhite[i - K][j] -
48+
startWhite[i][j - K] +
49+
startWhite[i - K][j - K];
50+
minChanges = Math.min(minChanges, blackSum, whiteSum);
51+
}
52+
}
53+
54+
console.log(minChanges);

0 commit comments

Comments
 (0)