SMALL
문제 이해가 잘안되는데 한글어렵네
const input = require("fs").readFileSync("/dev/stdin").toString().trim().split("\n");
const n = +input[0];
const candy = input.slice(1).map(v => v.split(""));
function swap(i, j, k) {
const coord = [[-1, 0], [1, 0], [0, -1], [0, 1]];
const here = candy[i][j];
if (candy[i+coord[k][0]]
&& candy[i+coord[k][0]][j+coord[k][1]]
&& here !== candy[i+coord[k][0]][j+coord[k][1]]) {
candy[i][j] = candy[i+coord[k][0]][j+coord[k][1]];
candy[i+coord[k][0]][j+coord[k][1]] = here;
return true;
} else return false;
}
function search() {
for (let l=0; l<2; l++) {
for (let x=0; x<n; x++) {
let count = 0;
let color = curCandy(x, 0, l);
for (let y=0; y<n; y++) {
if (curCandy(x, y, l) === color) {
count++;
if (count > maxCount) {
maxCount = count;
}
} else {
color = curCandy(x, y, l);
count = 1;
}
}
}
}
}
function curCandy(x, y, l) {
if (l === 0) return candy[x][y];
else return candy[y][x];
};
let maxCount = 0;
for (let i=0; i<n; i++) {
for (let j=0; j<n; j++) {
for (let k=0; k<4; k++) {
if (swap(i, j, k)) {
search()
swap(i, j, k)
}
}
}
}
console.log(maxCount);LIST
'코테 > 코테withJS' 카테고리의 다른 글
| 유용한 method(숫자, 문자) (0) | 2022.11.03 |
|---|---|
| 백준 1476번 날짜 계산 (0) | 2022.07.04 |
| 백준 2309번 일곱 난쟁이 (0) | 2022.07.02 |
| 백준 Node.js(자바스크립트) 11328 strfry문제 (0) | 2021.09.23 |
| 백준 Node.js(자바스크립트) 10808번 알파벳 개수(아스키코드) (0) | 2021.09.22 |