[Java] 백준-2447번 "별 찍기 - 10"

김빛나리·2021년 1월 8일

2447번 : 별 찍기 - 10



문제

재귀적인 패턴으로 별을 찍어 보자. N이 3의 거듭제곱(3, 9, 27, ...)이라고 할 때, 크기 N의 패턴은 N×N 정사각형 모양이다.
크기 3의 패턴은 가운데에 공백이 있고, 가운데를 제외한 모든 칸에 별이 하나씩 있는 패턴이다.

***
* *
***

N이 3보다 클 경우, 크기 N의 패턴은 공백으로 채워진 가운데의 (N/3)×(N/3) 정사각형을 크기 N/3의 패턴으로 둘러싼 형태이다. 예를 들어 크기 27의 패턴은 예제 출력 1과 같다.



입력

첫째 줄에 N이 주어진다. N은 3의 거듭제곱이다. 즉 어떤 정수 k에 대해 N=3k이며, 이때 1 ≤ k < 8이다.



출력

첫째 줄부터 N번째 줄까지 별을 출력한다.



예제

입력: 27
출력: 
***************************
* ** ** ** ** ** ** ** ** *
***************************
***   ******   ******   ***
* *   * ** *   * ** *   * *
***   ******   ******   ***
***************************
* ** ** ** ** ** ** ** ** *
***************************
*********         *********
* ** ** *         * ** ** *
*********         *********
***   ***         ***   ***
* *   * *         * *   * *
***   ***         ***   ***
*********         *********
* ** ** *         * ** ** *
*********         *********
***************************
* ** ** ** ** ** ** ** ** *
***************************
***   ******   ******   ***
* *   * ** *   * ** *   * *
***   ******   ******   ***
***************************
* ** ** ** ** ** ** ** ** *
***************************


내 소스 코드

import java.util.Scanner;

public class Main {
    static char[][] arr;

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n=0;

        n = s.nextInt();

        arr = new char[n][n];
        
        star(n, 0, 0, false);

        for(int i=0;i<n;i++) {
          for(int j=0;j<n;j++) {
            System.out.print(arr[i][j]);
          }
          System.out.println("");
        }
    }

    static void star(int n, int a, int b, boolean tf) {
      if(tf) {
        for(int i=a;i<a+n;i++) {
          for(int j=b;j<b+n;j++) {
            arr[i][j] = ' ';
          }
        }
      }
      else if(n == 1) arr[a][b] = '*';
      else {
        int cnt=0;
        for(int i=a;i<a+n;i+=n/3) {
          for(int j=b;j<b+n;j+=n/3) {
            if(cnt==4) star(n/3, i, j, true);
            else star(n/3, i, j, false);

            cnt++;
          }
        }
      }
    }
}

이렇게 코딩을 했더니 시간 초과가 나왔다. 27을 입력한 경우, System.out.print를 할 때 756번을 출력해야해서 오래걸렸던 것 같다. 그래서 System.out.print하기 전에 StringBuffer로 묶어주고, 한번만 출력하는 것으로 수정해주었다.

import java.util.Scanner;

public class Main {
    static char[][] arr;

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n=0;
        StringBuffer sb = new StringBuffer();

        n = s.nextInt();

        arr = new char[n][n];
        
        star(n, 0, 0, false);

        for(int i=0;i<n;i++) {
          for(int j=0;j<n;j++) {
            sb.append(arr[i][j]);
          }
          sb.append("\n");
        }

        System.out.print(sb);
    }

    static void star(int n, int a, int b, boolean tf) {
      if(tf) {
        for(int i=a;i<a+n;i++) {
          for(int j=b;j<b+n;j++) {
            arr[i][j] = ' ';
          }
        }
      }
      else if(n == 1) arr[a][b] = '*';
      else {
        int cnt=0;
        for(int i=a;i<a+n;i+=n/3) {
          for(int j=b;j<b+n;j+=n/3) {
            if(cnt==4) star(n/3, i, j, true);
            else star(n/3, i, j, false);

            cnt++;
          }
        }
      }
    }
}

0개의 댓글