티스토리 뷰

https://www.acmicpc.net/problem/3085

 

3085번: 사탕 게임

예제 3의 경우 4번 행의 Y와 C를 바꾸면 사탕 네 개를 먹을 수 있다.

www.acmicpc.net

 

 

 

const fs = require('fs');
const input = fs.readFileSync("./dev/stdin").toString().trim().split("\n");
const N = +input.shift()
const candy = input.map(v=>v.split(''))

let max = 1;
for(let i = 0; i<N; i++){
  if(max==N) break; // 최대값이면 그만함. 
  for(let j = 0; j<N; j++){
    if(max==N) break; // 최대값이면 그만함. 

    //안바꾸기
      let cntColumn = 1;
      for(let k = i-1; k>-1; k--){
        if(candy[k][j]==candy[i][j]){
          cntColumn++;
        }else{
          break;
        }
      }
      for(let k = i+1; k<N; k++){
        if(candy[k][j]==candy[i][j]){
          cntColumn++;
        }else{
          break;
        }
      }
      max = Math.max(cntColumn,max)

      let cntRow = 1;
      for(let k = j-1; k>-1; k--){
        if(candy[i][k]==candy[i][j]){
          cntRow++;
        }else{
          break;
        }
      }
      for(let k = j+1; k<N; k++){
        if(candy[i][k]==candy[i][j]){
          cntRow++;
        }else{
          break;
        }
      }
      max = Math.max(cntRow,max)




    /// 가로 바꾸기
    if(j+1<N && candy[i][j]!=candy[i][j+1]){
      let cnt1 = 1;
      for(let k = i-1; k>-1; k--){
        if(candy[k][j]==candy[i][j+1]){
          cnt1++;
        }else{
          break;
        }
      }
      for(let k = i+1; k<N; k++){
        if(candy[k][j]==candy[i][j+1]){
          cnt1++;
        }else{
          break;
        }
      }
      max = Math.max(cnt1,max)

      let cnt2 = 1;
      for(let k = i-1; k>-1; k--){
        if(candy[k][j+1]==candy[i][j]){
          cnt2++;
        }else{
          break;
        }
      }
      for(let k = i+1; k<N; k++){
        if(candy[k][j+1]==candy[i][j]){
          cnt2++;
        }else{
          break;
        }
      }
      max = Math.max(cnt2,max)

      let cnt3 = 1;
      for(let k = j-1; k>-1; k--){
        if(candy[i][k]==candy[i][j+1]){
          cnt3++;
        }else{
          break;
        }
      }
      max = Math.max(cnt3,max)

      let cnt4 = 1;
      for(let k = j+2; k<N; k++){
        if(candy[i][k]==candy[i][j]){
          cnt4++;
        }else{
          break;
        }
      }
      max = Math.max(cnt4,max)
    }

    //세로바꾸기
    if(i+1<N&&candy[i][j]!=candy[i+1][j]){
      let cnt1 = 1;
      for(let k = j-1; k>-1; k--){
        if(candy[i][k]==candy[i+1][j]){
          cnt1++;
        }else{
          break;
        }
      }
      for(let k = j+1; k<N; k++){
        if(candy[i][k]==candy[i+1][j]){
          cnt1++;
        }else{
          break;
        }
      }
      max = Math.max(cnt1,max)

      let cnt2 = 1;
      for(let k = j-1; k>-1; k--){
        if(candy[i+1][k]==candy[i][j]){
          cnt2++;
        }else{
          break;
        }
      }
      for(let k = j+1; k<N; k++){
        if(candy[i+1][k]==candy[i][j]){
          cnt2++;
        }else{
          break;
        }
      }
      max = Math.max(cnt2,max)


      let cnt3 = 1;
      for(let k = i-1; k>-1; k--){
        if(candy[k][j]==candy[i+1][j]){
          cnt3++;
        }else{
          break;
        }
      }
      max = Math.max(cnt3,max) 

      let cnt4 = 1;
      for(let k = i+2; k<N; k++){
        if(candy[k][j]==candy[i][j]){
          cnt4++;
        }else{
          break;
        }
      }
      max = Math.max(cnt4,max) 
    }
  }
}

console.log(max)

 

 

const fs = require('fs');
const input = fs.readFileSync("./dev/stdin").toString().trim().split("\n");
const N = +input[0]
let candy = input.splice(1).map(v=>v.split(''))

let max = 1;

for(let i = 0; i<N; i++){
  if(max==N)break;
  for(let j = 0; j<N; j++){
    if(max==N)break;
    candySwap(i,j)
  }
}
console.log(max)



function candySwap(i,j){
  const dir = [[0,1],[1,0]];
  dir.forEach(v=>{
    const [x,y] = v;

    if(i+x>-1 && j+y>-1 && i+x<N && j+y<N && candy[i+x][j+y]!=candy[i][j]){
        let temp = candy[i][j]
        candy[i][j] = candy[i+x][j+y]
        candy[i+x][j+y] = temp
        checkRow()
        checkColumn()    
        candy[i+x][j+y] = candy[i][j]
        candy[i][j] = temp
    }
  })
}

function checkRow(){
  for(let i = 0; i<N; i++){
    let checkArr = [1]
    for(let j = 1; j<N; j++){
      checkArr[j] = candy[i][j-1]==candy[i][j] ? checkArr[j-1]+1 :  1;
    }
    max = Math.max(...checkArr,max)
  }
}

function checkColumn(){
  for(let i = 0; i<N; i++){
    let checkArr = [1]
    for(let j = 1; j<N; j++){
      checkArr[j] = candy[j-1][i]==candy[j][i] ? checkArr[j-1]+1 : 1;
    }
    max = Math.max(...checkArr,max)
  }
}
728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함