๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • ์žฅ์›์ต ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ
๐Ÿ’ป Computer Science/- Data Structure, Algorithm

[์•Œ๊ณ ๋ฆฌ์ฆ˜ PS] ๋ฐฑ์ค€ 3085๋ฒˆ ์‚ฌํƒ• ๊ฒŒ์ž„ ์ž๋ฐ” ๋ฌธ์ œํ’€์ด

by Wonit 2021. 2. 14.

๋ฌธ์ œ

ํ•ด๋‹น ํฌ์ŠคํŒ…์€ ๋ฐฑ์ค€์˜ 3085๋ฒˆ ์‚ฌํƒ• ๊ฒŒ์ž„ ์˜ ์ ‘๊ทผ๊ณผ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•œ ๊ธ€ ์ž…๋‹ˆ๋‹ค.
์ •๋‹ต ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•˜์‹œ๋ ค๋ฉด solve url ์—์„œ ํ™•์ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ์ ‘๊ทผํ•ด์•ผ ํ•˜๋Š”์ง€๋ฅผ ๋จผ์ € ์ƒ๊ฐํ•ด๋ณด์ž.

๋ฌธ์ œ ์ ‘๊ทผ

๋ฌธ์ œ๋ฅผ ๋ณด๊ณ  ์šฐ๋ฆฌ๊ฐ€ ๋จผ์ € ํ•ด์•ผํ•  ์ผ์€ ๋ฌด์—‡์ผ๊นŒ?

 

๋ฐ”๋กœ ๊ฒฝ์šฐ์˜ ์ˆ˜์— ๋”ฐ๋ฅธ ์‹œ๊ฐ„ ๋ณต์žก๋„ ๊ณ„์‚ฐ์ด๋‹ค.

 

์ด ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ์–ด๋–ป๊ฒŒ ๋‚˜์˜ค๊ณ  ํ†ต๊ณผ๋ฅผ ํ•˜๋ƒ ๋งˆ๋ƒ์— ๋”ฐ๋ผ์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ํญ์ด ๋‹ค์–‘ํ•ด์ง„๋‹ค.

 

์ด๋ฒˆ ๋ฌธ์ œ์—์„œ๋Š” 2๊ฐœ์˜ ์กฐ๊ฑด์„ ์ƒ๊ฐํ•ด์„œ ๋ณด๋ฉด ๋œ๋‹ค.

  1. ์ธ์ ‘ํ•œ ์นธ์˜ ๋‘ ์›์†Œ๋ฅผ ๊ตํ™˜ํ•œ๋‹ค.
  2. ๊ฐ€์žฅ ๊ธด ์—ฐ์† ๋ถ€๋ถ„ ํ–‰,์—ด์„ ๊ณ ๋ฅธ ๋‹ค์Œ ๋จน๋Š”๋‹ค.

์ธ์ ‘ํ•œ ์นธ์˜ ๋‘ ์›์†Œ๋ฅผ ๊ตํ™˜ํ•œ๋‹ค.

n x n ํ–‰๋ ฌ์—์„œ ๋ชจ๋“  ์›์†Œ๊ฐ€ ์ธ์ ‘ํ•œ ์นธ์„ ๊ณ ๋ฅผ ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š” ์–ผ๋งˆ๋‚˜ ๋ ๊นŒ?

 

์•„๋งˆ 4 * n^2์ด ๋  ๊ฒƒ์ด๋‹ค.

 

ํ•˜์ง€๋งŒ ์ƒ๊ฐ์„ ํ•ด๋ณด๋ฉด 2 * n^2์œผ๋กœ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.


์™œ๋ƒํ•˜๋ฉด ์ž„์˜์˜ ํ•œ ์นธ์—์„œ๋Š” ๊ทธ ์ „ ์นธ์—์„œ ๊ตํ™˜์„ ํ–ˆ๋˜ ๊ณผ๊ฑฐ๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—

  • ์ด์ „์˜ ์™ผ์ชฝ์—์„œ ๊ตํ™˜๋œ ๊ฒฝ์šฐ
  • ์ด์ „์˜ ์œ„์—์„œ ๊ตํ™˜๋œ ๊ฒฝ์šฐ

๋ฅผ ์ œ์™ธํ•˜๋ฉด 2 * n^2์ด ๋  ๊ฒƒ์ด๋‹ค.

๊ฐ€์žฅ ๊ธด ์—ฐ์† ๋ถ€๋ถ„ ํ–‰,์—ด์„ ๊ณ ๋ฅธ ๋‹ค์Œ ๋จน๋Š”๋‹ค.

๊ฐ€์žฅ ๊ธด ์—ฐ์† ๋ถ€๋ถ„ ํ–‰, ์—ด์„ ์„ ํƒํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ํ•œ ์›์†Œ๊ฐ€ ๊ตํ™˜๋œ ํ›„ ํ•ด๋‹น ์›์†Œ์˜ ํ–‰๊ณผ ์—ด์„ ๊ฒ€์‚ฌํ•˜๋Š” ๋กœ์ง์œผ๋กœ ๋˜ํ•œ 2 * n^2์ด ์†Œ์š”๋œ๋‹ค.

 

๊ทธ๋Ÿผ ์ด ๋‘˜์„ ๋ชจ๋‘ ํ•ฉํ–ˆ์„ ๋•Œ, 4 * n^2 ์ด๋ฏ€๋กœ ์šฐ๋ฆฌ์˜ N์ด ์ตœ์•…์˜ ๊ฒฝ์šฐ 50์ด๋ผ๋ฉด ์ด 2500^2๋ฒˆ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋œ๋‹ค.

์–ด๋–ค ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์จ์•ผํ• ์ง€ ๋‚˜์™”๋‹ค.

n์— ๋Œ€ํ•ด ๋ชจ๋“  ๊ฒฝ์šฐ๊ฐ€ ์ถฉ๋ถ„ํžˆ ์ž‘์€ ๊ด€๊ณ„๋กœ ์šฐ๋ฆฌ๋Š” ์™„์ „ ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

์˜ค๋‹ต ํ›„๋ณด

์ •๋‹ต ์ฝ”๋“œ

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        int n = Integer.parseInt(br.readLine());

        char[][] board = new char[n][n];
        int answer = 0;

        int[] dx = {0, 1};
        int[] dy = {1, 0};

        for (int i = 0; i < n; i++) {
            char[] line = br.readLine().toCharArray();
            for (int j = 0; j < n; j++) {
                board[i][j] = line[j];
            }
        }


        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {

                if(j + 1 < n){
                    swap(board, j, j + 1, i, i);
                    answer = Math.max(answer, countCandy(board));
                    swap(board, j, j + 1, i, i);
                }
                if(i + 1 < n) {
                    swap(board, j, j, i, i + 1);
                    answer = Math.max(answer, countCandy(board));
                    swap(board, j, j, i, i + 1);
                }

            }
        }
        bw.write(String.valueOf(answer));
        bw.flush();
        bw.close();
    }

    private static int countCandy(char[][] arr) {
        int count = 1;

        for (int i = 0; i < arr.length; i++) {
            int temp = 1;
            for (int j = 1; j < arr.length; j++) {
                if(arr[i][j] == arr[i][j-1]) {
                    temp++;
                }
                else temp = 1;
                count = Math.max(count, temp);
            }
        }

        for (int i = 0; i < arr.length; i++) {
            int temp = 1;
            for (int j = 1; j < arr.length; j++) {
                if(arr[j][i] == arr[j-1][i]) {
                    temp++;
                }
                else temp = 1;
                count = Math.max(count, temp);
            }
        }
        return count;
    }

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

์ •๋‹ต ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•˜์‹œ๋ ค๋ฉด solve url ์—์„œ ํ™•์ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

๋Œ“๊ธ€