|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | + |
| 4 | +public class BJ_10933_별_찍기_18 { |
| 5 | + |
| 6 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 7 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 8 | + |
| 9 | + private static int N; |
| 10 | + private static int height; |
| 11 | + private static int width; |
| 12 | + |
| 13 | + private static char[][] mat; |
| 14 | + |
| 15 | + public static void main(String[] args) throws IOException { |
| 16 | + init(); |
| 17 | + sol(); |
| 18 | + } |
| 19 | + |
| 20 | + private static void init() throws IOException { |
| 21 | + N = Integer.parseInt(br.readLine()); |
| 22 | + height = (int) (Math.pow(2, N) - 1); |
| 23 | + width = height * 2 - 1; |
| 24 | + |
| 25 | + mat = new char[height][width]; |
| 26 | + } |
| 27 | + |
| 28 | + private static void sol() throws IOException { |
| 29 | + rec(N, 0, 0); |
| 30 | + printMat(); |
| 31 | + } |
| 32 | + |
| 33 | + private static void rec(int n, int x, int y) { |
| 34 | + if (n == 1) { |
| 35 | + mat[x][y] = '*'; |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + int h = (int) (Math.pow(2, n) - 1); |
| 40 | + int w = h * 2 - 1; |
| 41 | + |
| 42 | + if (n % 2 == 1) { |
| 43 | + for (int i = 0; i <= w / 2; i++) { |
| 44 | + mat[x + h - i - 1][y + i] = '*'; |
| 45 | + mat[x + h - i - 1][y + h * 2 - i - 2] = '*'; |
| 46 | + mat[x + h - 1][y + i] = '*'; |
| 47 | + mat[x + h - 1][y + h * 2 - i - 2] = '*'; |
| 48 | + } |
| 49 | + rec(n - 1, x + h / 2, y + w / 4 + 1); |
| 50 | + } else { |
| 51 | + for (int i = 0; i <= w / 2; i++) { |
| 52 | + mat[x][y + i] = '*'; |
| 53 | + mat[x][y + h * 2 - i - 2] = '*'; |
| 54 | + mat[x + i][y + i] = '*'; |
| 55 | + mat[x + i][y + h * 2 - i - 2] = '*'; |
| 56 | + } |
| 57 | + rec(n - 1, x + 1, y + w / 4 + 1); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private static void printMat() throws IOException { |
| 62 | + for (char[] chars : mat) { |
| 63 | + // 각 줄에서 마지막 '*'의 위치 찾기 |
| 64 | + int lastStar = -1; |
| 65 | + for (int i = chars.length - 1; i >= 0; i--) { |
| 66 | + if (chars[i] == '*') { |
| 67 | + lastStar = i; |
| 68 | + break; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // 마지막 별까지만 출력 |
| 73 | + for (int i = 0; i <= lastStar; i++) { |
| 74 | + if (chars[i] == '*') { |
| 75 | + bw.write('*'); |
| 76 | + } else { |
| 77 | + bw.write(' '); |
| 78 | + } |
| 79 | + } |
| 80 | + bw.write("\n"); |
| 81 | + } |
| 82 | + bw.flush(); |
| 83 | + bw.close(); |
| 84 | + br.close(); |
| 85 | + } |
| 86 | + |
| 87 | +} |
| 88 | +``` |
0 commit comments