ν‹°μŠ€ν† λ¦¬ λ·°

728x90
λ°˜μ‘ν˜•

문제

 

3085번: 사탕 κ²Œμž„

문제 μƒκ·Όμ΄λŠ” 어렸을 적에 "λ΄„λ³΄λ‹ˆ (Bomboni)" κ²Œμž„μ„ μ¦κ²¨ν–ˆλ‹€. κ°€μž₯ μ²˜μŒμ— N×N크기에 사탕을 μ±„μ›Œ λ†“λŠ”λ‹€. μ‚¬νƒ•μ˜ 색은 λͺ¨λ‘ 같지 μ•Šμ„ μˆ˜λ„ μžˆλ‹€. μƒκ·Όμ΄λŠ” μ‚¬νƒ•μ˜ 색이 λ‹€λ₯Έ μΈμ ‘ν•œ 두 칸을 κ³ 

www.acmicpc.net

 

문제 풀이

μΈμ ‘ν•œ 사탕을 swap ν•˜κ³ , κ°€λ‘œ μ„Έλ‘œμ—μ„œ μ—°μ†λœ μ‚¬νƒ•μ˜ 개수λ₯Ό μ²΄ν¬ν•˜λ©΄ ν•΄λ‹Ή 문제λ₯Ό ν’€ 수 μžˆλ‹€. λ‹€μŒκ³Ό 같은 경우의 수λ₯Ό νŒλ‹¨ν•˜μ—¬ 문제λ₯Ό μ ‘κ·Όν•˜λ©΄ μ‰½κ²Œ ν•΄κ²°ν•  수 μžˆλ‹€. λ˜ν•œ swap λ˜λŠ” 경우 λͺ¨λ“  λ²”μœ„λ₯Ό νƒμƒ‰ν•˜λŠ” 것이 μ•„λ‹Œ, swap λ˜λŠ” μ˜μ—­λ§Œ νƒμƒ‰ν•œλ‹€λ©΄ 속도λ₯Ό 높일 수 μžˆμ„ 것 κ°™λ‹€.

  • λŒ€κ°μ„ μ€ μΈμ ‘ν•œκ²ƒμœΌλ‘œ νŒλ‹¨ν•˜μ§€ μ•ŠλŠ”λ‹€.
    • 즉, κ°€λ‘œ μ„Έλ‘œμ— λŒ€ν•œ swap만 μ²˜λ¦¬ν•˜κ³  λΉ„κ΅ν•˜λ©΄ λœλ‹€.
  • N의 크기가 50μ΄λ―€λ‘œ 브루트 포슀둜 μΆ©λΆ„νžˆ 탐색 κ°€λŠ₯ν•œ 크기이닀.
    • μΈμ ‘ν•œ 사탕을 λ°°μ—΄μ˜ 순차적으둜 swap ν•˜κ³  νƒμƒ‰ν•˜λŠ” 것을 λ°˜λ³΅ν•˜μ—¬ 닡을 찾을 수 μžˆλ‹€.

μ½”λ“œ

Python μ½”λ“œ

from sys import stdin


def check_length():
    h_max, v_max = 1, 1

    for i in range(n):
        horizontal, vertical = 1, 1
        for j in range(n - 1):
            # κ°€λ‘œ 확인
            if candy[i][j] == candy[i][j + 1]:
                horizontal += 1
            else:
                h_max = \
                    horizontal if h_max < horizontal else h_max
                horizontal = 1
            # μ„Έλ‘œ 확인
            if candy[j][i] == candy[j + 1][i]:
                vertical += 1
            else:
                v_max = \
                    vertical if v_max < vertical else v_max
                vertical = 1

        h_max =\
            horizontal if h_max < horizontal else h_max
        v_max = \
            vertical if v_max < vertical else v_max

    return h_max if h_max > v_max else v_max


if __name__ == "__main__":
    n = int(stdin.readline())
    candy = [list(stdin.readline().rstrip()) for _ in range(n)]
    answer = 0
    for i in range(n):
        for j in range(n - 1):
            # κ°€λ‘œ swap
            candy[i][j], candy[i][j + 1] = candy[i][j + 1], candy[i][j]
            answer = max(answer, check_length())
            ret = check_length()
            answer = ret if answer < ret else answer
            candy[i][j], candy[i][j + 1] = candy[i][j + 1], candy[i][j]
            # μ„Έλ‘œ swap
            candy[j][i], candy[j + 1][i] = candy[j + 1][i], candy[j][i]
            ret = check_length()
            answer = ret if answer < ret else answer
            candy[j][i], candy[j + 1][i] = candy[j + 1][i], candy[j][i]

    print(answer)

Java μ½”λ“œ

import java.util.Scanner;

public class Main {
    static int check_length(char[][] candy) {
        int n = candy.length;
        int h_max = 1;
        int v_max = 1;
        int ret = 0;

        for (int i = 0; i < n; i++) {
            int horizontal = 1;
            int vertical = 1;
            for (int j = 0; j < n - 1; j++) {
                if (candy[i][j] == candy[i][j + 1]) {
                    horizontal += 1;
                } else {
                    if (h_max < horizontal) {
                        h_max = horizontal;
                    }
                    horizontal = 1;
                }
            }

            for (int j = 0; j < n - 1; j++) {
                if (candy[j][i] == candy[j + 1][i]) {
                    vertical += 1;
                } else {
                    if (v_max < vertical) {
                        v_max = vertical;
                    }
                    vertical = 1;
                }
            }
            if (h_max < horizontal) {
                h_max = horizontal;
            }

            if (v_max < vertical) {
                v_max = vertical;
            }
        }

        ret = v_max;

        if (h_max > v_max)
            ret = h_max;

        return ret;
    }

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        char[][] candy = new char[n][n];
        int answer = 0;

        for (int i = 0; i < n; i++) {
            candy[i] = s.next().toCharArray();
        }
        char temp;
        int ret;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n - 1; j++) {
                temp = candy[i][j];
                candy[i][j] = candy[i][j + 1];
                candy[i][j + 1] = temp;
                ret = check_length(candy);
                if (answer < ret) {
                    answer = ret;
                }
                temp = candy[i][j];
                candy[i][j] = candy[i][j + 1];
                candy[i][j + 1] = temp;
                temp = candy[j][i];
                candy[j][i] = candy[j + 1][i];
                candy[j + 1][i] = temp;
                ret = check_length(candy);
                if (answer < ret) {
                    answer = ret;
                }
                temp = candy[j][i];
                candy[j][i] = candy[j + 1][i];
                candy[j + 1][i] = temp;
            }
        }
        System.out.println(answer);
    }
}

μœ„μ™€ 같은 λ‘œμ§μ„ λ‹€λ₯Έ μ–Έμ–΄λ₯Ό μ‚¬μš©ν•œλ‹€λ©΄ μ‹œκ°„ 내에 톡과할 수 μžˆμ§€λ§Œ, νŒŒμ΄μ¬μ€ 톡과할 수 μ—†μœΌλ©° PyPy3λ₯Ό μ‚¬μš©ν•˜μ—¬μ•Ό ν•œλ‹€.

728x90
λ°˜μ‘ν˜•
λŒ“κΈ€
κΈ€ 보관함
μ΅œκ·Όμ— 올라온 κΈ€
μ΅œκ·Όμ— 달린 λŒ“κΈ€