본문 바로가기

OJ

[BOJ] 27495 만다라트 만들기 (JAVA)

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

 

27495번: 만다라트 만들기

1번째 줄에는 “#x. “를 출력한 다음, 사전순으로 가장 먼저 오는 중간 목표를 출력한다. 숫자가 알파벳보다 사전순으로 먼저 오고, 알파벳 대문자가 알파벳 소문자보다 사전순으로 먼저 온다. 2

www.acmicpc.net

전 이게 왜 이렇게 어려울까요?

샘플 테케에 있는 만다라트는 오타니가 만든 것 같습니다

 

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

public class Main {
	
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringBuilder sb = new StringBuilder();
    static StringTokenizer st;
    static String[][] input = new String[9][9];
    static int[] dr = { -1, -1, -1,  0,  0,  1,  1,  1 };
    static int[] dc = { -1,  0,  1, -1,  1, -1,  0,  1 };

    public static void main(String[] args) throws Exception {

        for (int i = 0; i < 9; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 0; j < 9; j++) {
                input[i][j] = st.nextToken();
            }
        }

        TreeMap<String, Integer> map = new TreeMap<>();
        TreeSet<String>[] set = new TreeSet[9];
        for (int i = 1; i < 9; i++) {
            set[i] = new TreeSet<>();
        }
        int idx = 1;
        for (int i = 1; i < 9; i += 3) {
            for (int j = 1; j < 9; j += 3) {
                if (i == 4 && j == 4) continue;
                map.put(input[i][j], idx);
                for (int dir = 0; dir < 8; dir++) {
                    int r = i + dr[dir];
                    int c = j + dc[dir];
                    set[idx].add(input[r][c]);
                }
                idx++;
            }
        }

        int cntMap = 1;
        while (!map.isEmpty()) {
            Map.Entry<String, Integer> entry = map.pollFirstEntry();
            sb.append("#").append(cntMap).append(". ").append(entry.getKey()).append("\n");
            int cntSet = 1;
            while (!set[entry.getValue()].isEmpty()) {
                sb.append("#").append(cntMap).append("-").append(cntSet++).append(". ").append(set[entry.getValue()].pollFirst()).append("\n");
            }
            cntMap++;
        }
        System.out.print(sb);

    }
	
}

'OJ' 카테고리의 다른 글