[백준3085] 사탕게임

yoontaeng·2022년 7월 14일
0
post-thumbnail

📎 문제링크

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

📄 문제설명

상근이는 어렸을 적에 "봄보니 (Bomboni)" 게임을 즐겨했다.

가장 처음에 N×N크기에 사탕을 채워 놓는다. 사탕의 색은 모두 같지 않을 수도 있다. 상근이는 사탕의 색이 다른 인접한 두 칸을 고른다. 그 다음 고른 칸에 들어있는 사탕을 서로 교환한다. 이제, 모두 같은 색으로 이루어져 있는 가장 긴 연속 부분(행 또는 열)을 고른 다음 그 사탕을 모두 먹는다.

사탕이 채워진 상태가 주어졌을 때, 상근이가 먹을 수 있는 사탕의 최대 개수를 구하는 프로그램을 작성하시오.

입력

첫째 줄에 보드의 크기 N이 주어진다. (3 ≤ N ≤ 50)

다음 N개 줄에는 보드에 채워져 있는 사탕의 색상이 주어진다. 빨간색은 C, 파란색은 P, 초록색은 Z, 노란색은 Y로 주어진다.

사탕의 색이 다른 인접한 두 칸이 존재하는 입력만 주어진다.

📝 문제풀이

입력받은 값으로 NxN의 2차원 배열을 만들어 주고 왼쪽,오른쪽,위쪽, 아래쪽으로 서로 값을 교환하는 함수를 만들어준다. 그다음 main에서 교환해주는 함수를 실행후 연속되는 최대의 값을 구하고 다시 반대교환 과정을 거쳐 원래대로 바꾸어 주어야 한다.

💡 Code

코드

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;


public class Main{

   static int max=0;
   static int N;
   public static void main(String[]args)throws IOException{
   BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
   N=Integer.parseInt(br.readLine()); // 크기 
   char[][]board=new char[N][N];
   for(int i=0;i<N;i++){
         String st=br.readLine();
       for(int j=0;j<N;j++){

        board[i][j]=st.charAt(j);

       }

   }

   for(int k=0;k<N;k++){

       
    for(int p=0;p<N;p++){
    
         swap1(board,k,p);          //왼쪽 
         mapping(board);
         swap2(board,k,p);
         
         swap2(board,k,p);         //오른쪽
         mapping(board);
         swap1(board,k,p);
        
         swap3(board,k,p);       //아래
         mapping(board);
         swap4(board,k,p);
    
         swap4(board,k,p);        //위
          mapping(board);
         swap3(board,k,p);
        }

      }
      
      System.out.println(max);
   
   }
 
   private static void swap1(char[][]b,int f,int r){   // 왼쪽 교환
        char temp;
        if(r!=0){
        temp=b[f][r];
        b[f][r]=b[f][r-1];
        b[f][r-1]=temp;
        }
   }
   private static void swap2(char[][]b,int f,int r){   // 오른쪽 교환
    char temp;
    if(r!=N-1){
    temp=b[f][r];
    b[f][r]=b[f][r+1];
    b[f][r+1]=temp;
       }
   }
    private static void swap3(char[][]b,int f,int r){   // 아래쪽 교환
        char temp;
        if(f!=0){
        temp=b[f][r];
        b[f][r]=b[f-1][r];
        b[f-1][r]=temp;
        }
   }
   private static void swap4(char[][]b,int f,int r){   // 위쪽 교환
    char temp;
    if(f!=N-1){
    temp=b[f][r];
    b[f][r]=b[f+1][r];
    b[f+1][r]=temp;
    }
 }
 private static int mapping(char[][]b){      // 최대의 연속된 수의 개수 찾기
    
    
    for(int i=0;i<b.length;i++){
        int count=1;  
        for(int j=0;j<N-1;j++){
            
             if(b[i][j]==b[i][j+1] ) count++;
             else count=1;     //초기화(연속되지 않은수를 만났을때)
            
             if(count>=max) max=count;
            
        }
    }
    for(int i=0;i<b.length;i++){
        int count=1;  
        for(int j=0;j<N-1;j++){
            
             if(b[j+1][i]==b[j][i]) count++;
             else count=1;
                 
             if(count>=max) max=count;
            
        }
    }
    return max;

 }
}

👍 Comment

Math.max 메소드로 구할수도 있음

profile
병아리개발자

0개의 댓글