알고리즘 문제풀이/백준
[백준 2447번] 별 찍기 - 10
슥지니
2021. 12. 27. 12:48
반응형
문제 링크: https://www.acmicpc.net/problem/2447
2447번: 별 찍기 - 10
재귀적인 패턴으로 별을 찍어 보자. N이 3의 거듭제곱(3, 9, 27, ...)이라고 할 때, 크기 N의 패턴은 N×N 정사각형 모양이다. 크기 3의 패턴은 가운데에 공백이 있고, 가운데를 제외한 모든 칸에 별이
www.acmicpc.net
<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
49
50
51
52
53
54
55
56
57
|
#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[6561][6561];
//x, y에서 시작해서 가로 세로의 크기가 각각 n
void solve(int x, int y, int n) {
if (n == 3) {
board[x][y] = '*';
board[x][y+1] = '*';
board[x][y+2] = '*';
board[x + 1][y] = '*';
board[x + 1][y + 1] = ' ';
board[x + 1][y + 2] = '*';
board[x + 2][y] = '*';
board[x + 2][y + 1] = '*';
board[x + 2][y + 2] = '*';
return;
}
solve(x, y, n / 3);
solve(x, y + n/3, n / 3);
solve(x, y + 2*n/3, n / 3);
solve(x + n/3, y, n / 3);
solve(x + n/3, y + 2*n/3, n / 3);
solve(x + 2*n/3, y, n / 3);
solve(x + 2*n/3, y + n/3, n / 3);
solve(x + 2*n/3, y + 2*n/3, n / 3);
}
int main(void) {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n;
fill(&board[0][0], &board[6560][6561], ' ');
solve(0, 0, n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << board[i][j];
}
cout << '\n';
}
return 0;
}
|
cs |
반응형