백준

백준 3085 - Java

으엉어엉 2024. 11. 12. 09:38
728x90

package silver;

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

public class BJ3085 {
    static int N;
    static char[][] board;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        N = Integer.parseInt(br.readLine());
        board = new char[N][N];

        for (int i = 0; i < N; i++) {
            String line = br.readLine();
            for (int j = 0; j < N; j++) {
                board[i][j] = line.charAt(j);
            }
        }

        int maxCandies = 0;

        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (j + 1 < N) {
                    swap(i, j, i, j + 1);
                    maxCandies = Math.max(maxCandies, checkMaxCandies());
                    swap(i, j, i, j + 1);
                }

                if (i + 1 < N) {
                    swap(i, j, i + 1, j);
                    maxCandies = Math.max(maxCandies, checkMaxCandies());
                    swap(i, j, i + 1, j);
                }
            }
        }

        System.out.println(maxCandies);
    }

    static void swap(int x1, int y1, int x2, int y2) {
        char temp = board[x1][y1];
        board[x1][y1] = board[x2][y2];
        board[x2][y2] = temp;
    }

    static int checkMaxCandies() {
        int max = 0;

        // 같은 행 체크
        for (int i = 0; i < N; i++) {
            int count = 1;
            for (int j = 1; j < N; j++) {
                if (board[i][j] == board[i][j - 1]) {
                    count++;
                } else {
                    max = Math.max(max, count);
                    count = 1;
                }
            }
            max = Math.max(max, count);
        }

        //같은열 체크
        for (int j = 0; j < N; j++) {
            int count = 1;
            for (int i = 1; i < N; i++) {
                if (board[i][j] == board[i - 1][j]) {
                    count++;
                } else {
                    max = Math.max(max, count);
                    count = 1;
                }
            }
            max = Math.max(max, count);
        }

        return max;
    }
}
728x90

'백준' 카테고리의 다른 글

백준 2110 - Java  (0) 2024.11.14
백준 11403 - Java  (0) 2024.11.13
백준 2309 - Java  (0) 2024.11.11
백준1475 - Java  (0) 2024.11.10
백준 9663 - Java  (1) 2024.11.09