반응형

문제 링크: https://www.acmicpc.net/problem/2448

 

2448번: 별 찍기 - 11

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

www.acmicpc.net

<문제 풀이> 재귀

위 그림처럼 3등분을 계속해서 n == 3일 때 (x, y)을 기준으로 별을 배열에 업데이트하면 됩니다.

 

 

<C++ 소스 코드>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <utility>
#include <climits>
#include <cmath>
#include <deque>
 
using namespace std;
 
char board[3072][6144];

//x,y에서 시작해서 높이가 n인 별 찍기
void solve(int x, int y, int n) {
    if (n == 3) {
        board[x][y] = '*';
        board[x+1][y-1] = '*';
        board[x + 1][y + 1] = '*';
        board[x + 2][y - 2] = '*';
        board[x + 2][y - 1] = '*';
        board[x + 2][y] = '*';
        board[x + 2][y + 1] = '*';
        board[x + 2][y + 2] = '*';
        return;
    }
    solve(x, y, n / 2);
    solve(x + n / 2, y - n / 2, n / 2);
    solve(x + n / 2, y + n / 2, n / 2);
 
}
 
int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n; cin >> n;
    fill(&board[0][0], &board[3071][6144], ' ');
    solve(0, n - 1, n);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < 2*n; j++) {
            cout << board[i][j];
        }
        cout << '\n';
    }
    return 0;
}
 
cs

 

 

반응형

+ Recent posts