코딩테스트/자바 문제풀이

[그리디] 백준 1343 폴리오미노

승요나라 2024. 8. 20. 20:38

1343번: 폴리오미노

https://www.acmicpc.net/problem/1343

 

# 코드

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        String input = br.readLine();

        int X_count = 0;
        for (int i = 0; i < input.length(); i++) {
            String sub = input.substring(i, i+1);
            if (sub.equals("X")) {
                // "X"가 나온 경우
                X_count++;
                if (X_count == 4) {
                    sb.append("AAAA");
                    X_count = 0;
                }
            } else {
                // "."이 나온 경우
                if (X_count == 4) {
                    sb.append("AAAA.");
                    X_count = 0;
                } else if (X_count == 2) {
                    sb.append("BB.");
                    X_count = 0;
                } else if (X_count != 0) {
                    System.out.println(-1);
                    return;
                } else {
                    sb.append(".");
                }
            }
        }

        // 반복문 종료 후 X_count 확인
        if (X_count == 4) {
            sb.append("AAAA");
            X_count = 0;
        } else if (X_count == 2) {
            sb.append("BB");
            X_count = 0;
        } else if (X_count != 0) {
            System.out.println(-1);
            return;
        }

        System.out.println(sb);
        br.close();
    }
}
  • 문제에서 주어진 로직대로 구현하면 크게 어렵지 않았던 문제였다. (~ ̄▽ ̄)~