문제 풀이 및 개발 공간

[백준] 3085번 사탕 게임 문제! (silver 2 본문

백준공부/java

[백준] 3085번 사탕 게임 문제! (silver 2

gomduri43 2023. 4. 27. 10:22

import java.io.*;
import java.util.*;

public class Main{
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        int n=Integer.parseInt(br.readLine());
        char[][] board=makeBoard(n);
        int answer=0;
        for(int i=0; i<n; i++){
            for(int j=0; j<n; j++){
                if(j>0) {
                    char temp = board[i][j];
                    board[i][j] = board[i][j - 1];
                    board[i][j - 1] = temp;
                    int tmp = Math.max(colMany(board, n), rowMany(board, n));
                    answer = answer > tmp ? answer : tmp;
                    board[i][j - 1] = board[i][j];
                    board[i][j] = temp;

                }
                if(i<n-1) {
                    char temp = board[i][j];
                    board[i][j] = board[i + 1][j];
                    board[i + 1][j] = temp;
                    int tmp = Math.max(colMany(board, n), rowMany(board, n));
                    answer = answer > tmp ? answer : tmp;
                    board[i + 1][j] = board[i][j];
                    board[i][j] = temp;

                }
            }
        }
        bw.write(answer+"");
        bw.flush();

    }
    //보드판 만들고 채워넣기
    public static char[][] makeBoard(int n) throws IOException{
        char[][] board=new char[n][n];
        for(int i=0; i<n; i++){
            String a=br.readLine();
            for(int j=0; j<n; j++){
                board[i][j]=a.charAt(j);
            }
        }
        return board;
    }

    //가로열
    public static int rowMany(char[][] board, int n){
        int max=0;
        for(int i=0; i<n; i++){
            char tempc=board[i][0];
            int temp=0;
            for(int j=0; j<n; j++){
               if(board[i][j]==tempc){
                   temp++;
               }
               else{
                   max= max > temp ? max:temp;
                   temp=1;
                   tempc=board[i][j];
               }
            }
            max= max > temp ? max:temp;
        }
        return max;
    }
    //세로열 검사
    public static int colMany(char[][] board, int n){
        int max=0;
        for(int j=0; j<n; j++){
            char tempc=board[0][j];
            int temp=0;
            for(int i=0; i<n; i++){
                if(board[i][j]==tempc){
                    temp++;
                }
                else{
                    max= max > temp ? max:temp;
                    temp=1;
                    tempc=board[i][j];
                }
            }
            max= max > temp ? max:temp;
        }
        return max;
    }

}



//조건을 제대로 안읽어서 오랜 헤맨문제,,, 연속하는 사탕을 먹는거였음