https://www.acmicpc.net/problem/3085
3085번: 사탕 게임
예제 3의 경우 4번 행의 Y와 C를 바꾸면 사탕 네 개를 먹을 수 있다.
www.acmicpc.net
문장을 그대로 코드로 구현하면 된다.
완전탐색으로 풀면 된다.
function calc(arr, x,y){
let lineX = 0
let lineY = 0
for(let i = x; i<n; i++){
if(arr[i][y] == arr[x][y]){
lineX++
}else break
}
for(let i = y; i<n; i++){
if(arr[x][i]==arr[x][y]){
lineY++;
}else break
}
return Math.max(lineX, lineY);
}
let p = require('fs').readFileSync('js/example.txt').toString().split('\n');
const n = parseInt(p[0]);
let arr = p.slice(1).map(v => v.split(""));
let arrY = Array.from(Array(n), () => Array(n).fill(''))
for(let i = 0; i<n; i++){
for(let j = 0; j<n; j++){
arrY[i][j] = arr[i][j]
}
}
let ans = 0;
for(let x = 0; x < n; x++){
for(let y = 0; y<n; y++){
ans = Math.max(ans,calc(arr,x,y))
}
}
for(let i = 0; i<n-1; i++){
for(let j = 0; j<n; j++){
let tmp = arr[i+1][j]
arr[i+1][j] = arr[i][j]
arr[i][j] = tmp // 인접한 x축을 바꾼 후 계산
for(let x = 0; x < n; x++){
for(let y = 0; y<n; y++){
ans = Math.max(ans,calc(arr,x,y))
}
}
for(let x = 0; x < n; x++){
for(let y = 0; y<n; y++){
arr[x][y] = arrY[x][y]
}
}
}
}
let a = ans
for(let i = 0; i<n; i++){
for(let j = 0; j<n-1; j++){
let tmp = arr[i][j+1]
arr[i][j+1] = arr[i][j]
arr[i][j] = tmp // 인접한 y축을 바꾼 후 계산
for(let x = 0; x < n; x++){
for(let y = 0; y<n; y++){
ans = Math.max(ans,calc(arr,x,y))
}
}
for(let x = 0; x < n; x++){
for(let y = 0; y<n; y++){
arr[x][y] = arrY[x][y]
}
}
}
}
console.log(Math.max(a,ans))
'ps' 카테고리의 다른 글
[백준 2916] 도영이가 만든 맛있는 음식 [JS] (0) | 2024.01.11 |
---|---|
[백준] 15656 N과 M (7) Node js 풀이 (0) | 2024.01.07 |
[백준] - 1064 평행사변형 (0) | 2024.01.02 |
[백준] 11000 강의실 배정 (0) | 2023.06.15 |
[백준] 9935 문자열 폭발 (0) | 2023.06.11 |