끄적끄적 코딩
article thumbnail

분할정복 문제입니다.

  *
 * *
*****
위의 모양이 반복되는 것을 확인할 수 있습니다.

n과 0, 0을 넣고 solve함수를 실행합니다.
n이 1이되면 위의 모양을 그대로 넣어줍니다.
1이 아니면 n/2를 해주고, y와 x에 대해서
위 가운데, 왼쪽 아래, 오른쪽 아래로 나누어서
solve 함수를 실행합니다.

이를 반복하여서 map에 *을 전부 그리고 출력합니다.

 

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

char DB[3][6] =
{ "  *  ",
  " * * ",
  "*****" };
char map[4000][8000];

void solve(int n, int y, int x) 
{
	if (n == 1)
	{
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 5; j++) {
				map[y + i][x + j] = DB[i][j];
			}
		}
		return;
	}

	solve(n / 2, y, x + 3 * n / 2);
	solve(n / 2, y + 3 * n / 2, x);
	solve(n / 2, y + 3 * n / 2, x + 3 * n);

	return;
}

int main(int argc, char *argv[])
{
	int n;

	cin >> n;

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n * 2; ++j) {
			map[i][j] = ' ';
		}
	}

	solve(n / 3, 0, 0);

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n * 2; ++j) {
			cout << map[i][j];
		}
		cout << endl;
	}


	return 0;
}

검색 태그