별 찍기 - 11

Huisu·2024년 1월 6일
0

Coding Test Practice

목록 보기
92/98
post-thumbnail

문제

2448번: 별 찍기 - 11

문제 설명

예제를 보고 규칙을 유추한 뒤에 별을 찍어 보세요.

제한 사항

첫째 줄에 N이 주어진다. N은 항상 3×2k 수이다. (3, 6, 12, 24, 48, ...) (0 ≤ k ≤ 10, k는 정수)

입출력 예

아이디어

재귀

제출 코드

import java.io.IOException;
import java.util.Scanner;

public class four2448 {
    public int n;
    public char[][] tree;

    public void solution() throws IOException {
        Scanner sc = new Scanner(System.in);
        StringBuilder sb = new StringBuilder();
        n = sc.nextInt();
        tree = new char[n][n * 2 - 1];

        for (int i = 0; i < tree.length; i++) {
            for (int j = 0; j < tree[0].length; j++) {
                tree[i][j] = ' ';
            }
        }

        drawWall(0, n - 1); // 꼭대기 좌표

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n * 2 - 1; j++) {
                sb.append(tree[i][j]);
            }
            sb.append('\n');
        }
        System.out.println(sb.toString());
    }

    private void drawWall(int col, int row) {
        drawOneTree(col, row);
        int length = 3;

        while (length <= n / 2) {
            copy(col, row, length, col + length, row - length);
            copy(col, row, length, col + length, row + length);
            length *= 2;
        }
    }

    private void copy(int col, int row, int length, int newCol, int newRow) {
        for (int i = 0; i < length; i++) {
            for (int j = -(length - 1); j < length; j++) {
                tree[newCol + i][newRow + j] = tree[col + i][row + j];
            }
        }
    }

    private void drawOneTree(int col, int row) {
        tree[col][row] = '*';

        tree[col + 1][row - 1] = '*';
        tree[col + 1][row + 1] = '*';

        tree[col + 2][row - 2] = '*';
        tree[col + 2][row - 1] = '*';
        tree[col + 2][row] = '*';
        tree[col + 2][row + 1] = '*';
        tree[col + 2][row + 2] = '*';
    }

    public static void main(String[] args) throws IOException {
        new four2448().solution();
    }
}

0개의 댓글