https://www.acmicpc.net/problem/10993
재귀를 통한 별찍기 문제
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
char output[1100][2100];
void init() {
fill(&output[0][0], &output[1099][2100], ' ');
}
void solve(int x, int y, int n) {
if (n == 0) return;
int row = pow(2, n + 1) - 3;
int col = pow(2, n) - 1;
if (n % 2 == 0) {
for (int i = y; i < y + row; i++) output[x][i] = '*';
for (int i = x; i < x + col; i++) {
output[i][y + i - x] = '*';
output[i][y + row - i + x - 1] = '*';
}
solve(x + 1, y + row / 2, n - 1);
}
else {
for (int i = x; i < x + col; i++) {
output[i][y + x - i] = '*';
output[i][y + i - x] = '*';
}
for (int i = y-row/2; i < y + row/2; i++) output[x+col-1][i] = '*';
solve(x + col / 2, y - col / 2+1, n - 1);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
init();
int n;
cin >> n;
int row = pow(2, n + 1) - 3;
int col = pow(2, n) - 1;
if (n % 2 == 0) solve(0, 0, n);
else solve(0, row / 2, n);
for (int i = 0; i < col; i++) {
if (n % 2 == 0) {
for (int j = 0; j < row-i; j++) {
cout << output[i][j];
}
}
else {
for (int j = 0; j < row/2+i+1; j++) {
cout << output[i][j];
}
}
cout << '\n';
}
}
예전에 풀었던 별찍기 11 문제와 비슷하거나 조금 더 까다로웠다.