1018. 체스판 다시 칠하기 - node.js / javascript
문제
내 코드
let fs = require("fs");
let input = fs.readFileSync("/dev/stdin").toString().trim().split("\n");
const NM = input.shift().split(" ");
const N = Number(NM.shift());
const M = Number(NM.shift());
const candidates = [];
const whiteFirstBoard = [
"WBWBWBWB",
"BWBWBWBW",
"WBWBWBWB",
"BWBWBWBW",
"WBWBWBWB",
"BWBWBWBW",
"WBWBWBWB",
"BWBWBWBW",
];
const blackFirstBoard = [
"BWBWBWBW",
"WBWBWBWB",
"BWBWBWBW",
"WBWBWBWB",
"BWBWBWBW",
"WBWBWBWB",
"BWBWBWBW",
"WBWBWBWB",
];
function paintWhiteFirst(x, y) {
let count = 0;
for (let i = y; i < y + 8; i++) {
for (let j = x; j < x + 8; j++) {
if (input[i][j] !== whiteFirstBoard[i - y][j - x]) {
count++;
}
}
}
return count;
}
function paintBlackFirst(x, y) {
let count = 0;
for (let i = y; i < y + 8; i++) {
for (let j = x; j < x + 8; j++) {
if (input[i][j] !== blackFirstBoard[i - y][j - x]) {
count++;
}
}
}
return count;
}
for (let i = 0; i + 7 < N; i++) {
for (let j = 0; j + 7 < M; j++) {
candidates.push(paintWhiteFirst(j, i));
candidates.push(paintBlackFirst(j, i));
}
}
console.log(Math.min(...candidates));
깃허브 링크
let fs = require("fs");
let input = fs.readFileSync("/dev/stdin").toString().trim().split("\n");
const NM = input.shift().split(" ");
const N = Number(NM.shift());
const M = Number(NM.shift());
const candidates = [];
const whiteFirstBoard = [
"WBWBWBWB",
"BWBWBWBW",
"WBWBWBWB",
"BWBWBWBW",
"WBWBWBWB",
"BWBWBWBW",
"WBWBWBWB",
"BWBWBWBW",
];
const blackFirstBoard = [
"BWBWBWBW",
"WBWBWBWB",
"BWBWBWBW",
"WBWBWBWB",
"BWBWBWBW",
"WBWBWBWB",
"BWBWBWBW",
"WBWBWBWB",
];
function paintWhiteFirst(x, y) {
let count = 0;
for (let i = y; i < y + 8; i++) {
for (let j = x; j < x + 8; j++) {
if (input[i][j] !== whiteFirstBoard[i - y][j - x]) {
count++;
}
}
}
return count;
}
function paintBlackFirst(x, y) {
let count = 0;
for (let i = y; i < y + 8; i++) {
for (let j = x; j < x + 8; j++) {
if (input[i][j] !== blackFirstBoard[i - y][j - x]) {
count++;
}
}
}
return count;
}
for (let i = 0; i + 7 < N; i++) {
for (let j = 0; j + 7 < M; j++) {
candidates.push(paintWhiteFirst(j, i));
candidates.push(paintBlackFirst(j, i));
}
}
console.log(Math.min(...candidates));
Author And Source
이 문제에 관하여(1018. 체스판 다시 칠하기 - node.js / javascript), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@arthur/1018.-체스판-다시-칠하기-node.js-javascript저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)