알고리즘 도전기 - 9

김치전사·2022년 3월 19일
0

알고리즘 도전기

목록 보기
9/89

3085 사탕 게임

문제

상근이는 어렸을 적에 "봄보니 (Bomboni)" 게임을 즐겨했다.
가장 처음에 N×N크기에 사탕을 채워 놓는다. 사탕의 색은 모두 같지 않을 수도 있다. 상근이는 사탕의 색이 다른 인접한 두 칸을 고른다. 그 다음 고른 칸에 들어있는 사탕을 서로 교환한다. 이제, 모두 같은 색으로 이루어져 있는 가장 긴 연속 부분(행 또는 열)을 고른 다음 그 사탕을 모두 먹는다.
사탕이 채워진 상태가 주어졌을 때, 상근이가 먹을 수 있는 사탕의 최대 개수를 구하는 프로그램을 작성하시오.

입력

첫째 줄에 보드의 크기 N이 주어진다. (3 ≤ N ≤ 50)
다음 N개 줄에는 보드에 채워져 있는 사탕의 색상이 주어진다. 빨간색은 C, 파란색은 P, 초록색은 Z, 노란색은 Y로 주어진다.
사탕의 색이 다른 인접한 두 칸이 존재하는 입력만 주어진다.

출력

첫째 줄에 상근이가 먹을 수 있는 사탕의 최대 개수를 출력한다.

예제입력

3
CCP
CCP
PPC

예제출력

3

코드

import java.util.*;

public class Main{
    public static String[][] candyList;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        candyList = new String[N][N];

        for(int i=0;i<N;i++){
                candyList[i]=scanner.next().split("");
        }

        int result = changeCandy(candyList,N);

        System.out.println(result);
    }

    public static int changeCandy(String[][] candyList,int N){
        int maxValue = 0;

        for(int i=0;i<N;i++) {
            for (int j=0; j<N; j++) {
                int colCount=1;
                int rowCount=1;
                String tmp;
                if(j!=N-1){
                    tmp = candyList[i][j];
                    candyList[i][j] = candyList[i][j + 1];
                    candyList[i][j + 1] = tmp;
                }

                for(int n=0;n<N;n++){
                    for(int m=0;m<N;m++){

                        String answer = candyList[n][m];

                        if(m>0&&answer.equals(candyList[n][m-1])){
                            rowCount+=1;
                            if(maxValue<rowCount){
                                maxValue=rowCount;
                            }
                        }else{
                            rowCount=1;
                        }

                        String colAnswer = candyList[m][n];
                        if(m>0&&colAnswer.equals(candyList[m-1][n])){
                            colCount+=1;
                            if(maxValue<colCount){
                                maxValue=colCount;
                            }
                        }else{
                            colCount=1;
                        }
                    }
                }
                if(j!=N-1){
                    tmp = candyList[i][j];
                    candyList[i][j] = candyList[i][j + 1];
                    candyList[i][j + 1] = tmp;
                }

            }
        }

        for(int i=0;i<N;i++) {
            for (int j = 0; j<N; j++) {
                int colCount=1;
                int rowCount=1;
                String tmp;
                if(i!=N-1){
                    tmp = candyList[i][j];
                    candyList[i][j] = candyList[i+1][j];
                    candyList[i+1][j] = tmp;
                }

                for(int n=0;n<N;n++){
                    for(int m=0;m<N;m++){

                        String answer = candyList[n][m];

                        if(m>0&&answer.equals(candyList[n][m-1])){
                            rowCount+=1;
                            if(maxValue<rowCount){
                                maxValue=rowCount;
                            }
                        }else{
                            rowCount=1;
                        }

                        String colAnswer = candyList[m][n];
                        if(m>0&&colAnswer.equals(candyList[m-1][n])){
                            colCount+=1;
                            if(maxValue<colCount){
                                maxValue=colCount;
                            }
                        }else{
                            colCount=1;
                        }
                    }
                }
                if(i!=N-1){
                    tmp = candyList[i][j];
                    candyList[i][j] = candyList[i+1][j];
                    candyList[i+1][j] = tmp;
                }

            }
        }
        return maxValue;
    }
}
profile
개인공부 블로그입니다. 상업적 용도 X

0개의 댓글