문제
https://www.acmicpc.net/problem/3085
정답 코드
const fs = require("fs");
const filePath = process.platform === "linux" ? "/dev/stdin" : "./input.txt";
const input = fs.readFileSync(filePath).toString().trim().split("\n");
const checkMaxCount = (candy, maxCount) => {
// 행체크
for (let row of candy) {
let count = 1;
for (let i = 0; i < candy.length - 1; i++) {
if (row[i] === row[i + 1]) {
count++;
} else {
maxCount = Math.max(maxCount, count);
count = 1;
}
maxCount = Math.max(maxCount, count);
}
}
// 열체크
for (let col = 0; col < candy[0].length - 1; col++) {
let count = 1;
for (let row = 0; row < candy.length - 1; row++) {
if (candy[row][col] === candy[row + 1][col]) {
count++;
} else {
maxCount = Math.max(maxCount, count);
count = 1;
}
}
maxCount = Math.max(maxCount, count);
}
return maxCount;
};
function solution(input) {
const N = Number(input[0]);
const candy = input.filter((_, index) => index !== 0).map((c) => c.split(""));
let maxCount = 0;
for (let i = 0; i < N; i++) {
for (let j = 0; j < N; j++) {
// 오른쪽
if (j + 1 < N && candy[i][j] !== candy[i][j + 1]) {
// 바꾼다
const target = candy[i][j];
candy[i][j] = candy[i][j + 1];
candy[i][j + 1] = target;
maxCount = checkMaxCount(candy, maxCount);
// 되돌리기
candy[i][j + 1] = candy[i][j];
candy[i][j] = target;
}
// 아래쪽
if (i + 1 < N && candy[i][j] !== candy[i + 1][j]) {
// 바꾼다
const target = candy[i][j];
candy[i][j] = candy[i + 1][j];
candy[i + 1][j] = target;
maxCount = checkMaxCount(candy, maxCount);
// 되돌리기
candy[i + 1][j] = candy[i][j];
candy[i][j] = target;
}
}
}
return maxCount;
}
console.log(solution(input));
브루트포스 문제... 모든 요소를 탐색하면서 바꿔준 다음에 행과 열 을 다시 탐색하며 숫자를 세주어야한다..
'코딩문제풀기' 카테고리의 다른 글
[백준] 2309 | 일곱 난쟁이 | 자바스크립트 (0) | 2025.01.08 |
---|---|
[백준] 10799 | 쇠막대기 | 자바스크립트 (1) | 2025.01.03 |
[백준] 2525 | 오븐 시계 | 자바스크립트 (0) | 2025.01.03 |
[백준] 10866 | 덱 | 자바스크립트 (0) | 2024.12.30 |
[백준] 14681 | 사분면 고르기 | 자바스크립트 (0) | 2024.12.30 |