1일1알

백준 2448번 별 찍기 - 11 C++ 본문

알고리즘

백준 2448번 별 찍기 - 11 C++

영춘권의달인 2022. 3. 27. 12:28

출처 : https://www.acmicpc.net/problem/2448

 

삼각형의 높이, 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";
	}
};

'알고리즘' 카테고리의 다른 글

백준 2096번 내려가기 C++  (0) 2022.03.31
백준 9935번 문자열 폭발 C++  (0) 2022.03.28
백준 4963번 섬의 개수 C++  (0) 2022.03.26
백준 1120번 문자열 C++  (0) 2022.03.24
백준 1475번 방 번호 C++  (0) 2022.03.23