View
https://www.acmicpc.net/problem/2448
2448번: 별 찍기 - 11
첫째 줄에 N이 주어진다. N은 항상 3×2k 수이다. (3, 6, 12, 24, 48, ...) (0 ≤ k ≤ 10, k는 정수)
www.acmicpc.net
문제 요약
은 들어가서 예제 확인...
문제 해결 아이디어
일단 제일 최소 단위의 형태가
*
* *
*****
라고 생각했기에 재귀를 돌려 주면서 기저조건에 해당할 때, 즉 cnt가 3일 때 해당 모양을 찍어 주며 재귀를 돌면 된다고 생각했다.
숫자가 한단계씩 늘어날 때마다, 즉 N이 2배 될때마다 삼각형의 개수?는 3배가 되기 때문에 재귀를 돌 때 index를 가지고 돌면서 그 index를 맨 위 삼각형의 시작, 왼쪽 아래 삼각형의 시작, 오른쪽 아래 삼각형의 시작으로 바꿔 주면 된다고 생각했다.
배열은 모두 공백으로 초기화를 해 뒀고 N개의 행에 N*2-1개의 열이 나온다.
완성된 코드
package week3;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main_BOJ_2448_별찍기11 {
/**
*
552ms
*/
static int N;
static char[][] arr;
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
N = Integer.parseInt(br.readLine());
arr = new char[N][N * 2 - 1];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
arr[i][j] = ' ';
}
}
print(0, 0, N);
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
sb.append(arr[i][j]);
}
sb.append("\n");
}
System.out.println(sb);
}
public static void print(int r, int c, int cnt) {
if (cnt == 3) {
arr[r][c + 2] = '*';
arr[r + 1][c + 1] = '*';
arr[r + 1][c + 3] = '*';
for (int i = c; i < c+5; i++)
arr[r+2][i] = '*';
return;
}
print(r, c + cnt/2, cnt / 2);
print(r + cnt/2, c, cnt / 2);
print(r + cnt/2, c+cnt, cnt / 2);
}
}
'Level-Up > 알고리즘' 카테고리의 다른 글
[백준] 7490. 0 만들기 - 골드5 (0) | 2021.09.01 |
---|---|
[백준] 2606. 바이러스 - 실버3 (0) | 2021.09.01 |
[백준] 14888. 연산자 끼워넣기 - 실버1 (0) | 2021.08.25 |
[백준] 5639. 이진 검색 트리 - 실버1 (0) | 2021.08.25 |
[백준] 10815. 숫자 카드 - 실버4 (0) | 2021.08.25 |
reply