in.add()
[3085] 사탕 게임 본문
문제 링크 : https://www.acmicpc.net/problem/3085
3085번: 사탕 게임
예제 3의 경우 4번 행의 Y와 C를 바꾸면 사탕 네 개를 먹을 수 있다.
www.acmicpc.net
문제 해결 방법
완전 탐색으로 오른쪽 사탕과 바꿨을 때, 아래 사탕과 바꿨을 때 최대 연속된 개수를 구하는 방법으로 구현했다.
import java.util.Scanner;
public class Main {
    static char[][] candies;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        candies = new char[N][N];
        sc.nextLine();
        for (int i = 0; i < N; i++) {
            candies[i] = sc.nextLine().toCharArray();
        }
        int max = 0;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (j < N - 1) {
                    swapRight(i, j); // 오른쪽 사탕과 바꿈
                    int result = count(); // 숫자 세기
                    max = Math.max(max, result);
                    swapRight(i, j); // 원래대로
                }
                if (i < N - 1) {
                    swapDown(i, j); // 아래 사탕과 바꿈
                    int result = count(); // 숫자 세기
                    max = Math.max(max, result);
                    swapDown(i, j); // 원래대로
                }
            }
        }
        System.out.println(max);
    }
    public static int count() { // 가장 긴 연속된 사탕 수 찾기
        int n = candies.length;
        int result = 1;
        for (int i = 0; i < n; i++) {
            int cnt = 1;
            for (int j = 0; j < n - 1; j++) {
                if (candies[i][j] == candies[i][j + 1]) {
                    cnt++;
                } else {
                    cnt = 1;
                }
                result = Math.max(result, cnt);
            }
            cnt = 1;
            for (int j = 0; j < n - 1; j++) {
                if (candies[j][i] == candies[j + 1][i]) {
                    cnt++;
                } else {
                    cnt = 1;
                }
                result = Math.max(result, cnt);
            }
        }
        return result;
    }
    public static void swapRight(int i, int j) {
        if (!(candies[i][j] == candies[i][j + 1])) {
            char tmp = candies[i][j];
            candies[i][j] = candies[i][j + 1];
            candies[i][j + 1] = tmp;
        }
    }
    public static void swapDown(int i, int j) {
        if (!(candies[i][j] == candies[i + 1][j])) {
            char tmp = candies[i][j];
            candies[i][j] = candies[i + 1][j];
            candies[i + 1][j] = tmp;
        }
    }
}'Algorithm > BOJ' 카테고리의 다른 글
| [2116] 주사위 쌓기 (0) | 2021.09.11 | 
|---|---|
| [2012] 등수 매기기 (0) | 2021.09.10 | 
| [16236] 아기 상어 (0) | 2021.09.04 | 
| [14889] 스타트와 링크 (0) | 2021.09.03 | 
| [1654] 랜선 자르기 (0) | 2021.09.02 | 
			  Comments