PS
백준 2447번 별 찍기 - 10
남마허
2021. 4. 16. 17:53
2447번: 별 찍기 - 10
재귀적인 패턴으로 별을 찍어 보자. N이 3의 거듭제곱(3, 9, 27, ...)이라고 할 때, 크기 N의 패턴은 N×N 정사각형 모양이다. 크기 3의 패턴은 가운데에 공백이 있고, 가운데를 제외한 모든 칸에 별이
www.acmicpc.net
<접근방법>
분할정복
<코드>
#include <iostream>
#include <algorithm>
#include <math.h>
using namespace std;
#define MAX 2200
int n;
char map[MAX][MAX];
void solve(int y, int x, int k) {
if (k == 1) {
int t = 0;
for (int i = y; i < y + 3; i++) {
for (int j = x; j < x + 3; j++) {
t++;
if (t == 5) {
continue;
}
map[i][j] = '*';
}
}
return;
}
int len = pow(3, k);
int t = 0;
for (int i = y; i < y + len; i += (len / 3)) {
for (int j = x; j < x + len; j += (len / 3)) {
t++;
if (t == 5) continue;
solve(i, j, k-1);
}
}
}
int main() {
cin >> n;
int k = 0;
while (n / 3 != 0) {
k++;
n /= 3;
}
solve(0, 0, k);
int len = pow(3, k);
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
if (map[i][j] == '*')
cout << map[i][j];
else cout << ' ';
}
cout << '\n';
}
return 0;
}
반응형