문제 번호
알고리즘 분류
브루트포스
문제 풀이
정상적으로 칠해진 8*8 크기의 체스판 2개를 미리 생성했다. 검은색부터 칠해진것 하나, 흰색부터 칠해진것 하나.
let white = ['WBWBWBWB', 'BWBWBWBW', 'WBWBWBWB', 'BWBWBWBW', 'WBWBWBWB', 'BWBWBWBW', 'WBWBWBWB', 'BWBWBWBW']
let black = ['BWBWBWBW', 'WBWBWBWB', 'BWBWBWBW', 'WBWBWBWB', 'BWBWBWBW', 'WBWBWBWB', 'BWBWBWBW', 'WBWBWBWB']
문제에서 주어진 체스판을 (0,0) 좌표부터 (M - 8, N - 8)좌표 까지 탐색하면서 8*8크기로 때어내어 정상인 체스판과 비교했다.
for (let i = 0; i <= N - 8; i++) {
for (let j = 0; j <= M - 8; j++) {
check(j, i);
}
}
function check(x, y) {
let checkWhite = 0;
let checkBlack = 0;
for (let i = y; i < y + 8; i++) {
for (let j = x; j < x + 8; j++) {
if (board[i][j] !== white[i-y][j-x])
checkWhite++;
if (board[i][j] !== black[i-y][j-x])
checkBlack++;
}
}
let min = checkBlack < checkWhite ? checkBlack : checkWhite;
if (min < answer)
answer = min;
}
전체코드
const fs = require('fs');
let input = fs.readFileSync('/dev/stdin').toString().split('\n');
let NM = input.shift().split(' ');
let N = Number(NM.shift());
let M = Number(NM.shift());
let white = ['WBWBWBWB', 'BWBWBWBW', 'WBWBWBWB', 'BWBWBWBW', 'WBWBWBWB', 'BWBWBWBW', 'WBWBWBWB', 'BWBWBWBW']
let black = ['BWBWBWBW', 'WBWBWBWB', 'BWBWBWBW', 'WBWBWBWB', 'BWBWBWBW', 'WBWBWBWB', 'BWBWBWBW', 'WBWBWBWB']
let board = [];
for (let i = 0; i < N; i++) {
board[i] = input.shift().split('');
}
let answer = 90;
for (let i = 0; i <= N - 8; i++) {
for (let j = 0; j <= M - 8; j++) {
check(j, i);
}
}
function check(x, y) {
let checkWhite = 0;
let checkBlack = 0;
for (let i = y; i < y + 8; i++) {
for (let j = x; j < x + 8; j++) {
if (board[i][j] !== white[i-y][j-x])
checkWhite++;
if (board[i][j] !== black[i-y][j-x])
checkBlack++;
}
}
let min = checkBlack < checkWhite ? checkBlack : checkWhite;
if (min < answer)
answer = min;
}
console.log(answer);
특이사항
'Algorithm > BaeKJoon' 카테고리의 다른 글
[JS][백준]1260_DFS와 BFS (0) | 2021.08.24 |
---|---|
[JS][백준]1436_영화감독 숌 (0) | 2021.08.19 |
[JS][백준]7568_덩치 (0) | 2021.08.18 |
[JS][백준]2231_분해합 (0) | 2021.08.18 |
[JS][백준]2798_블랙잭 (0) | 2021.08.18 |