본문 바로가기
백준 알고리즘/시뮬레이션

백준 1022번 C++

by paysmile 2019. 8. 30.

#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>

using namespace std;
struct Move {
	int x, y, z;
};
Move mv[4] = { {0,1,1},{-1,0,1},{0,-1,1},{1,0,1} };

int main(void) {
	int r1, c1, r2, c2;
	int maxrange;
	int map[50][5];
	cin >> r1 >> c1 >> r2 >> c2;
	maxrange = max(max(abs(r1), abs(c1)), max(abs(r2), abs(c2)));
	if (abs(0 - r1) >= 0 && abs(0 - r1) <= (r2-r1+1) && abs(0 - c1) >= 0 && abs(0 - c1) <= (c2-c1+1)) 
		map[abs(0 - r1)][abs(0 - c1)] = 1;

	int counts = 1, flag = 0, maxlength=-1;
	int lastx = 0;
	int lasty = 0;
	int lastnum = 1;
	while (1 && flag == 0) {

		for (int k = 0; k < 4; k++) {
			if (lastx >= maxrange && lasty >= maxrange) {
				flag = 1;
				break;
			}
			for (int i = counts; i > 0; i--) {
				lastx = lastx + mv[k].x;
				lasty = lasty + mv[k].y;
				lastnum = lastnum + mv[k].z;

				if (abs(lastx-r1) >= 0 && abs(lastx-r1) <= (r2-r1+1) && abs(lasty-c1) >= 0 && abs(lasty-c1) <= (c2-c1+1)) {
					map[abs(lastx - r1)][abs(lasty - c1)] = lastnum;
					maxlength = max(maxlength, lastnum);
				}
			}
			if (k == 1 || k == 3)
				counts++;
			if (flag == 1)
				break;
		}
	}
	int lengths = 1;
	while (maxlength / 10 != 0) {
		maxlength = maxlength / 10;
		lengths++;
	}
	for (int i = 0; i < (r2 - r1 + 1); i++) {
		for (int j = 0; j < (c2 - c1 + 1); j++) {
			printf("%*d ", lengths, map[i][j]);
		}
		cout << endl;
	}
	return 0;
}

'백준 알고리즘 > 시뮬레이션' 카테고리의 다른 글

백준 1152번 C++  (0) 2019.08.30
백준 1551번 C++  (0) 2019.08.30
백준 1526번 C++  (0) 2019.08.29
백준 2979번 C++  (0) 2019.08.29
백준 11559번 C++  (0) 2019.08.29