알고리즘
백준 2448번 별 찍기 - 11 C++
영춘권의달인
2022. 3. 27. 12:28
삼각형의 높이, row, col 정보를 이용하여 재귀를 통해 문제를 해결하였다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
using namespace std;
using ll = long long;
void rec(int height, int row, int col, vector<vector<char>> &board) {
if (height == 3) {
board[row][col] = '*';
board[row + 1][col - 1] = '*';
board[row + 1][col + 1] = '*';
for (int i = col - 2; i <= col + 2; i++) {
board[row + 2][i] = '*';
}
return;
}
rec(height / 2, row, col, board);
rec(height / 2, row + height / 2, col - height / 2, board);
rec(height / 2, row + height / 2, col + height / 2, board);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
vector<vector<char>> board(n, vector<char>(n * 2 - 1, ' '));
rec(n, 0, n - 1, board);
for (auto a : board) {
for (auto b : a) {
cout << b;
}
cout << "\n";
}
};