[Java] 백준 1343 폴리오미노

Lee GaEun·2025년 1월 31일

[Java] 알고리즘

목록 보기
54/93

1343 폴리오미노 문제 링크

문제


#1

import java.awt.*;
import java.io.*;
import java.util.*;

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

        System.out.println(board());
    }

    static String board() {
        int countX = 0;
        String answer = "";

        for(int i=0; i<poly.length(); i++) {
            if(poly.charAt(i) == 'X') {
                countX++;
            }
            else if(poly.charAt(i) == '.') {
                if(countX != 0) {
                    for (int j = 0; j < countX / 4; j++) {
                        answer += "AAAA";
                    }
                    countX = countX % 4;
                    for (int j = 0; j < countX / 2; j++) {
                        answer += "BB";
                    }
                    if (countX % 2 != 0) return "-1";
                    countX = 0;
                }
                answer += ".";
            }
        }
        if(countX != 0) {
            for (int j = 0; j < countX / 4; j++) {
                answer += "AAAA";
            }
            countX = countX % 4;
            for (int j = 0; j < countX / 2; j++) {
                answer += "BB";
            }
            if (countX % 2 != 0) return "-1";
            countX = 0;
        }

        return answer;
    }
}

  • 성공!
  • 근데 코드가 넘 더럽다..

  • 김주희 코드 보니까 replace로 풀면 되더라,,,,
profile
I will give it my all (๑•̀o•́๑)ง

0개의 댓글