2 minute read


0. 들어가면서

별찍기-10 문제이다.
재귀 문제이다.

1. Java

1.1 내가 푼 코드(시간 초과)

import java.util.*;

public class Main {
    /*
     * N * N 꼴 일 때
     * 시작 위치를 알아야해 그리는건 8개짜리로
     */
    static int[][] map;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        map = new int[N][N];

        drawStar(N, 0, 0);
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (map[i][j] == 1)
                    System.out.print("*");
                if (map[i][j] == 0)
                    System.out.print(" ");
            }
            System.out.println();
        }
    }

    private static void drawStar(int n, int y, int x) {
        if (n == 3) { // base condition
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    map[y + i][x + j] = 1;
                }
            }
            map[y + 1][x + 1] = 0; // 빈칸 뚫기
            return;
        }

        for (int i = 0; i < 3; i++) { // 8개 실행 해야지, 시작 포인트 8개 뽑는겨
            for (int j = 0; j < 3; j++) {
                if (i == 1 && j == 1)// 가운데는 뚫어야지
                    continue;

                drawStar(n / 3, y + (n / 3) * i, x + (n / 3) * j);
            }
        }
    }
}

1.2 내가 푼 코드(성공)

package bj.ch11_recursion;

import java.util.*;

public class BJ_2447_별찍기10_01 {
    /*
     * N * N 꼴 일 때
     * 시작 위치를 알아야해 그리는건 8개짜리로
     */
    static int[][] map;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        map = new int[N][N];

        drawStar(N, 0, 0);

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (map[i][j] == 1)
                    sb.append("*");
                if (map[i][j] == 0)
                    sb.append(" ");
            }
            sb.append('\n');
        }
        System.out.print(sb.toString());
    }

    private static void drawStar(int n, int y, int x) {
        if (n == 3) { // base condition
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    map[y + i][x + j] = 1;
                }
            }
            map[y + 1][x + 1] = 0; // 빈칸 뚫기
            return;
        }

        for (int i = 0; i < 3; i++) { // 8개 실행 해야지, 시작 포인트 8개 뽑는겨
            for (int j = 0; j < 3; j++) {
                if (i == 1 && j == 1)// 가운데는 뚫어야지
                    continue;

                drawStar(n / 3, y + (n / 3) * i, x + (n / 3) * j);
            }
        }
        return;
    }
}


1.3 해설

  • 방법은 맞았지만 시간초과가 떠서 난감했다.
  • 이유는 System.out.println()이 시간을 많이 잡아먹는다고 한다.
  • 그래서 StringBuilder를 사용해서 한번에 출력하도록 했다.
  • 이거 꼭 기억하자!

2. Go

CLOSING

출력방법 1: System.out.println(): 시간을 꽤나 잡아 먹어!
출력방법 2: StringBuilder: 모아서 한번에 출력!
기억하자!

Leave a comment