[BOJ 3085] ์‚ฌํƒ•๊ฒŒ์ž„

 

1. ๋ฌธ์ œ : Link

CCP

CCT

SCW

์ค‘์— ์—ฐ์†ํ•˜๋Š” ๋‘ ๊ฐ’์„ ๊ตํ™˜ํ–ˆ์„๋•Œ ์ตœ๋Œ€๋กœ ์—ฐ์†ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์—์„œ์˜ ์ˆซ์ž๋ฅผ return

 

2. ํ’€์ด

brute-force๋ฌธ์ œ์ด๋ฏ€๋กœ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•œ๋‹ค.

๋จผ์ € check ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ์—ฐ์†ํ•˜๋Š” ๊ฐ’์˜ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•œ๋‹ค.

checkํ•จ์ˆ˜์— ๋„ฃ๋Š” ์ธ์ž๋Š” ๊ตํ™˜ ํ›„์˜ ๊ฐ’์„ ๋„ฃ๋Š”๋‹ค.

 

3. ์ฝ”๋“œ

package baekjoon;

import java.util.Scanner;

public class Main {
    static int N;
    static String[][] map;
    static int answer = 0;

    static int maxVal = 0;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        N = sc.nextInt();

        map = new String[N][N];

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

        String temp = "";
        int cnt = 0;
        String cur = "";

        //๊ฐ€๋กœ
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N - 1; j++) {
                temp = map[i][j];
                map[i][j] = map[i][j + 1];
                map[i][j + 1] = temp;
                for (int a = 0; a < N; a++) {
                    cnt = 0;
                    cur = map[a][0];
                    for (int b = 0; b < N; b++) {
                        if (cur.equals(map[a][b])) {
                            cnt++;
                            maxVal = Math.max(maxVal, cnt);
                        } else {
                            cur = map[a][b];
                            maxVal = Math.max(maxVal, cnt);
                            cnt = 1;
                        }
                    }
                }
                for (int a = 0; a < N; a++) {
                    cnt = 0;
                    cur = map[0][a];
                    for (int b = 0; b < N; b++) {
                        if (cur.equals(map[b][a])) {
                            cnt++;
                            maxVal = Math.max(maxVal, cnt);
                        } else {
                            cur = map[b][a];
                            maxVal = Math.max(maxVal, cnt);
                            cnt = 1;
                        }
                    }
                }
                map[i][j + 1] = map[i][j];
                map[i][j] = temp;
            }
        }

        //์„ธ๋กœ
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N - 1; j++) {
                temp = map[j][i];
                map[j][i] = map[j + 1][i];
                map[j + 1][i] = temp;
                for (int a = 0; a < N; a++) {
                    cnt = 0;
                    cur = map[a][0];
                    for (int b = 0; b < N; b++) {
                        if (cur.equals(map[a][b])) {
                            cnt++;
                            maxVal = Math.max(maxVal, cnt);
                        } else {
                            cur = map[a][b];
                            maxVal = Math.max(maxVal, cnt);
                            cnt = 1;
                        }
                    }
                }
                for (int a = 0; a < N; a++) {
                    cnt = 0;
                    cur = map[0][a];
                    for (int b = 0; b < N; b++) {
                        if (cur.equals(map[b][a])) {
                            cnt++;
                            maxVal = Math.max(maxVal, cnt);
                        } else {
                            cur = map[b][a];
                            maxVal = Math.max(maxVal, cnt);
                            cnt = 1;
                        }
                    }
                }
                map[j + 1][i] = map[j][i];
                map[j][i] = temp;
            }
        }
        System.out.println(maxVal);
    }
}

 

  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ