๊ธฐ๋ก๋ฐฉ

BOJ_3085 : ์‚ฌํƒ• ๊ฒŒ์ž„ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_3085 : ์‚ฌํƒ• ๊ฒŒ์ž„

Soom_1n 2023. 1. 15. 17:35

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

3085๋ฒˆ: ์‚ฌํƒ• ๊ฒŒ์ž„

์˜ˆ์ œ 3์˜ ๊ฒฝ์šฐ 4๋ฒˆ ํ–‰์˜ Y์™€ C๋ฅผ ๋ฐ”๊พธ๋ฉด ์‚ฌํƒ• ๋„ค ๊ฐœ๋ฅผ ๋จน์„ ์ˆ˜ ์žˆ๋‹ค.

www.acmicpc.net



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • n X n ๋กœ ์‚ฌํƒ•์ด ์žˆ๋‹ค.
    • ์‚ฌํƒ•์˜ ์ข…๋ฅ˜๋Š” C, P, Z, Y๊ฐ€ ์žˆ๋‹ค.
    • ์ธ์ ‘ํ•œ ๋‘ ์นธ์„ ๊ณจ๋ผ ์‚ฌํƒ•์„ ๊ตํ™˜ํ•œ๋‹ค.
    • ๊ฐ™์€ ์ƒ‰์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ํ–‰ ๋˜๋Š” ์—ด ์ค‘ ๋จน์„ ์ˆ˜ ์žˆ๋Š” ์‚ฌํƒ•์˜ ์ตœ๋Œ€๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.
  • ํ’€์ด
    • ์ตœ๋Œ€๊ฐ’์„ ์ฐพ๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ตํ™˜ํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ฐพ์•„์•ผ ํ•œ๋‹ค.
      • ์‚ฌํƒ• ๊ตํ™˜์€ ์•„๋ž˜ ํ˜น์€ ์˜ค๋ฅธ์ชฝ์œผ๋กœ๋งŒ ์ด๋™ํ•˜๋ฉฐ ์ง„ํ–‰ํ•œ๋‹ค.
      • ๊ฐ๊ฐ์˜ ๊ฒฝ์šฐ์— ๋‚˜์˜ค๋Š” ์—ฐ์†๋œ ์‚ฌํƒ•์˜ ์ตœ๋Œ€๊ฐ’์„ ์ €์žฅํ•œ๋‹ค.
      • ์‚ฌํƒ•์˜ ์ตœ์†Œ๊ฐ’์€ 1์ด๋‹ค.
    • ์ตœ๋Œ€๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import java.util.Scanner;

public class Main {
    static char[][] arr;

    private static int countCandy(int n, int i, int j, int order, int max) {
        char[][] candyes = new char[n][n];

        for (int k = 0; k < n; k++) { // 2์ค‘ ๋ฐ˜๋ณต๋ฌธ
            for (int l = 0; l < n; l++) {
                candyes[k][l] = arr[k][l];
            }
        }

        if (order == 0) {
            char temp = candyes[i][j];
            candyes[i][j] = candyes[i + 1][j];
            candyes[i + 1][j] = temp;
        } else {
            char temp = candyes[i][j];
            candyes[i][j] = candyes[i][j + 1];
            candyes[i][j + 1] = temp;
        }

        for (int k = 0; k < n; k++) {
            int count = 1;
            for (int l = 1; l < n; l++) {
                if (candyes[k][l] == candyes[k][l - 1]) {
                    count++;
                } else {
                    if (count > max) {
                        max = count;
                    }
                    count = 1;
                }
            }
            if (count > max) {
                max = count;
            }
        }

        for (int k = 0; k < n; k++) {
            int count = 1;
            for (int l = 1; l < n; l++) {
                if (candyes[l][k] == candyes[l - 1][k]) {
                    count++;
                } else {
                    if (count > max) {
                        max = count;
                    }
                    count = 1;
                }
            }
            if (count > max) {
                max = count;
            }
        }

        return max;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        arr = new char[n][n];
        for (int i = 0; i < n; i++) {
            String string = sc.next();
            for (int j = 0; j < n; j++) {
                arr[i][j] = string.charAt(j);
            }
        }

        int answer = 1;
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n; j++) {
                answer = countCandy(n, i, j, 0, answer);
            }
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n - 1; j++) {
                answer = countCandy(n, i, j, 1, answer);
            }
        }
        System.out.println(answer);
    }
}

 

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • main ๋ฉ”์†Œ๋“œ์—์„œ n X n ์‚ฌํƒ•์„ ์ž…๋ ฅ๋ฐ›์•„ charํ˜• ์ด์ฐจ์› ๋ฐฐ์—ด arr์— ์ €์žฅํ•œ๋‹ค.
    • ์•„๋ž˜์ชฝ ์‚ฌํƒ•๊ณผ ๊ตํ™˜ํ•  ๋•Œ๋Š” ํ–‰์„ n-1๊ฐœ ํƒ์ƒ‰ํ•œ๋‹ค.
    • ์˜ค๋ฅธ์ชฝ ์‚ฌํƒ•๊ณผ ๊ตํ™˜ํ•  ๋•Œ๋Š” ์—ด์„ n-1๊ฐœ ํƒ์ƒ‰ํ•œ๋‹ค.
  • countCandy ๋ฉ”์†Œ๋“œ์—์„œ ์—ฐ์†๋œ ์‚ฌํƒ•์˜ ์ตœ๋Œ€๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    • arr๋ฅผ candyes ๋ฐฐ์—ด์— ๋ณต์‚ฌํ•œ๋‹ค.
    • order๊ฐ€ 0์ด๋ฉด ์•„๋ž˜์ชฝ, ์•„๋‹ˆ๋ผ๋ฉด ์˜ค๋ฅธ์ชฝ๊ณผ ์‚ฌํƒ•์„ ๊ตํ™˜ํ•œ๋‹ค.
    • ํ•œ ํ–‰์—์„œ ๊ฐ€์žฅ ๊ธด ์—ฐ์† ์‚ฌํƒ•์˜ ์ˆ˜๋ฅผ ํ™•์ธํ•ด answer์™€ ๋น„๊ตํ•œ๋‹ค.
    • ํ•œ ์—ด์—์„œ ๊ฐ€์žฅ ๊ธด ์—ฐ์† ์‚ฌํƒ•์˜ ์ˆ˜๋ฅผ ํ™•์ธํ•ด answer์™€ ๋น„๊ตํ•œ๋‹ค.
  • answer๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ์ฒ˜์Œ๋ถ€ํ„ฐ ํ’€์ด์™€ ๊ฐ™์€ ์ ‘๊ทผ ๋ฐฉ๋ฒ•์„ ์ƒ๊ฐํ–ˆ์ง€๋งŒ, ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๊ฑฑ์ •๋๋Š”๋ฐ ๋ถ€๋ฅดํŠธํฌ์Šค ํƒœ๊ทธ๋ฅผ ๋ณด๊ณ  ๋ฏฟ์Œ์„ ๊ฐ€์กŒ๋‹ค.
  • countCandy ๋ฉ”์†Œ๋“œ์—์„œ ํ–‰๊ณผ ์—ด์˜ ๊ฐ€์žฅ ๊ธด ์—ฐ์† ์‚ฌํƒ•์˜ ์ˆ˜๋ฅผ ์ฐพ๋Š” for๋ฌธ์—์„œ max๊ฐ’์„ ์ €์žฅํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋น ๋œจ๋ ค์„œ 1ํšŒ ํ‹€๋ ธ๋‹ค.
    • ์ด์ค‘ for๋ฌธ์—์„œ ์•ˆ์ชฝ for๋ฌธ์ด ๋๋‚˜์•ผ count๋ฅผ max์™€ ๋น„๊ตํ–ˆ์—ˆ๋Š”๋ฐ, ๋น„๊ตํ•˜๋Š” ์‚ฌํƒ•์ด ์„œ๋กœ ๋‹ค๋ฅผ ๋•Œ์—๋„ max์™€ ๋น„๊ตํ•ด์„œ ์ €์žฅํ•ด์•ผํ–ˆ๋‹ค.
    • ๊ฒฐ๊ณผ์ ์œผ๋กœ max๋น„๊ต๋ฌธ์ด 2๋ฒˆ์”ฉ ๋“ค์–ด๊ฐ€์•ผํ•œ๋‹ค.

728x90

'CodingTest > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ_10610 : 30  (0) 2023.01.16
BOJ_2960 : ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด  (0) 2023.01.16
BOJ_2304 : ์ฐฝ๊ณ  ๋‹ค๊ฐํ˜•  (0) 2023.01.15
BOJ_10972 : ๋‹ค์Œ ์ˆœ์—ด  (0) 2023.01.14
BOJ_2563 : ์ƒ‰์ข…์ด  (0) 2023.01.13