[삼성 SW 역량 테스트]나무 재태크





#include <stdio.h>
#include <algorithm>
using namespace std;

struct TREE {
	int y, x, age;
};

bool cmp(TREE& a, TREE& b) {
	return (a.age < b.age);
}

struct QUEUE {
	int f, b;
	TREE tree[684000];

	QUEUE() {
		init();
	}

	void init() {
		f = 0, b = 0;
	}

	bool isempty() {
		return (f == b);
	}

	void push(TREE val) {
		tree[b++] = val;
	}

	void pop() {
		++f;
	}

	TREE front() {
		return tree[f];
	}

	int size() {
		return (b - f);
	}
};

int n, m, k;
QUEUE tree[2];
QUEUE new_tree;
QUEUE dead_tee, birth_of_child_tree;

TREE init_tree[100];

int map[10][10], add[10][10];

int main()
{
	scanf("%d %d %d", &n, &m, &k);
	for (int y = 0; y < n; ++y) {
		for (int x = 0; x < n; ++x) {
			scanf("%d", &add[y][x]);
			map[y][x] = 5;
		}
	}

	for (int i = 0; i < m; ++i) {
		scanf("%d %d %d", &init_tree[i].y, &init_tree[i].x, &init_tree[i].age);
		init_tree[i].y--, init_tree[i].x--;
	}

	sort(init_tree, init_tree + m, cmp);
	
	int cur = 0, next = 0;
	for (int i = 0; i < m; ++i) {
		tree[cur].push(init_tree[i]);
	}
	
	for (int i = 0; i < k; ++i) {

		next = (cur + 1) % 2;
		
		dead_tee.init();
		birth_of_child_tree.init();
		tree[next].init();

		while (!new_tree.isempty()) {
			TREE cur_tree = new_tree.front(); new_tree.pop();

			if (map[cur_tree.y][cur_tree.x] >= cur_tree.age) {
				map[cur_tree.y][cur_tree.x] -= cur_tree.age;

				++cur_tree.age;
				tree[next].push(cur_tree);
			}
			else {
				dead_tee.push(cur_tree);
			}
		}

		while (!tree[cur].isempty()) {
			TREE cur_tree = tree[cur].front(); tree[cur].pop();

			if (map[cur_tree.y][cur_tree.x] >= cur_tree.age) {
				map[cur_tree.y][cur_tree.x] -= cur_tree.age;

				++cur_tree.age;
				tree[next].push(cur_tree);

				if ((cur_tree.age % 5) == 0) {
					birth_of_child_tree.push(cur_tree);
				}
			}
			else {
				dead_tee.push(cur_tree);
			}
		}

		while (!dead_tee.isempty()) {
			TREE cur_tree = dead_tee.front();	dead_tee.pop();
			map[cur_tree.y][cur_tree.x] += (cur_tree.age / 2);
		}

		const int dy[] = { -1, -1, -1, 0, 0, +1, +1, +1 };
		const int dx[] = { -1, 0, +1, -1, +1, -1, 0, +1 };

		new_tree.init();
		while (!birth_of_child_tree.isempty()) {
			TREE cur_tree = birth_of_child_tree.front();	birth_of_child_tree.pop();
			for (int dir = 0; dir < 8; ++dir) {
				TREE next_tree;
				next_tree.y = cur_tree.y + dy[dir];
				next_tree.x = cur_tree.x + dx[dir];
				next_tree.age = 1;
				if (next_tree.y < 0 || next_tree.y >= n || next_tree.x < 0 || next_tree.x >= n) {
					continue;
				}
				new_tree.push(next_tree);
			}
		}

		for (int y = 0; y < n; ++y) {
			for (int x = 0; x < n; ++x) {
				map[y][x] += add[y][x];
			}
		}

		cur = next;
	}

	printf("%d\n", tree[next].size() + new_tree.size());

	return 0;
}


설정

트랙백

댓글

[삼성 SW 역량 테스트] 인구 이동





#include <stdio.h>
#include <algorithm>
#include <queue>
using namespace std;

struct POSI {
	int y, x;
};

int n, l, r;
int map[50][50];

void create_area(int sy, int sx, int status[][50], int index, int& count, int& sum) {
	int visited[50][50] = { 0, };

	const int dy[] = { 0, 0, -1, +1 };
	const int dx[] = { -1, +1, 0, 0 };

	queue<posi> q;
	POSI head;
	head.y = sy;
	head.x = sx;
	visited[sy][sx] = 1;
	q.push(head);

	while (!q.empty()) {
		POSI cur = q.front();	q.pop();

		status[cur.y][cur.x] = index;
		++count;
		sum += map[cur.y][cur.x];

		for (int dir = 0; dir < 4; ++dir) {
			POSI next;
			next.y = cur.y + dy[dir];
			next.x = cur.x + dx[dir];

			if (next.y < 0 || next.y >= n || next.x < 0 || next.x >= n) {
				continue;
			}

			int delta = abs(map[cur.y][cur.x] - map[next.y][next.x]);
			if (visited[next.y][next.x] == 0 && l <= delta && delta <= r) {
				visited[next.y][next.x] = 1;
				q.push(next);
			}
		}
	}
}


int main()
{
	scanf("%d %d %d", &n, &l, &r);
	for (int y = 0; y < n; ++y) {
		for (int x = 0; x < n; ++x) {
			scanf("%d", &map[y][x]);
		}
	}

	int ret = 0;
	bool is_update = true;
	while (is_update) {
		is_update = false;

		int status[50][50] = { 0, };
		int area_index = 0;
		int count[2501] = { 0, };
		int sum[2501] = { 0, };
		for (int y = 0; y < n; ++y) {
			for (int x = 0; x < n; ++x) {
				if (status[y][x] == 0) {
					++area_index;
					create_area(y, x, status, area_index, count[area_index], sum[area_index]);
				}
			}
		}

		for (int y = 0; y < n; ++y) {
			for (int x = 0; x < n; ++x) {
				int  index = status[y][x];
				int avg = sum[index] / count[index];
				if (map[y][x] != avg) {
					map[y][x] = avg;
					is_update = true;					
				}
			}
		}
		if (is_update) {
			++ret;
		}
	}

	printf("%d\n", ret);
	return 0;
}


설정

트랙백

댓글

[삼성 SW 역량 테스트] 치킨 배달





#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;

struct POSI {
	int y, x;
};

int n, m, type, ret;
vector<POSI> house, shop, pick;

void dfs(int pos) {
	if (pick.size() == m) {

		int candi = 0;
		for (int i = 0; i < house.size(); ++i) {
			int min_dist = 1000000;
			for (int j = 0; j < pick.size(); ++j) {
				min_dist = min(min_dist,
					abs(house[i].y - pick[j].y) + abs(house[i].x - pick[j].x));
			}
			candi += min_dist;
		}

		if (ret > candi) {
			ret = candi;
		}

		return;
	}

	for (int i = pos; i < shop.size(); ++i) {
		pick.push_back(shop[i]);
		dfs(i + 1);
		pick.pop_back();
	}
}


int main()
{
	POSI target;
	scanf("%d %d", &n, &m);
	for (int y = 0; y < n; ++y) {
		for (int x = 0; x < n; ++x) {
			scanf("%d", &type);
			if (type == 1) {
				target.y = y, target.x = x;
				house.push_back(target);
			}
			if (type == 2) {
				target.y = y, target.x = x;
				shop.push_back(target);
			}
		}
	}

	ret = 0x7fffffff;
	dfs(0);
	printf("%d\n", ret);

	return 0;
}


설정

트랙백

댓글

[삼성 SW 역량 테스트] 드래곤 커브





#include <stdio.h>

const int dy[] = { 0, -1, 0, +1 };
const int dx[] = { +1, 0, -1, 0 };

int main()
{
	int n;
	int map[101][101] = { 0, };

	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		
		int curve_size = 0;
		int curve[1024] = { 0, };

		int x, y, d, g;
		scanf("%d %d %d %d", &x, &y, &d, &g);

		curve[curve_size++] = d;
		map[y][x] = 1;

		for (int j = 0; j < g; ++j) {
			for (int k = curve_size - 1; k >= 0; --k) {
				curve[curve_size++] = (curve[k] + 1) % 4;
			}
		}

		for (int j = 0; j < curve_size; ++j) {
			y += dy[curve[j]];
			x += dx[curve[j]];
			if (y < 0 || y >= 101 || x < 0 || x > 101) {
				continue;
			}
			map[y][x] = 1;
		}
	}

	int ret = 0;
	for (int y = 0; y < 100; ++y) {
		for (int x = 0; x < 100; ++x) {
			if (map[y][x] && map[y][x + 1] && map[y + 1][x] && map[y + 1][x + 1]) {
				++ret;
			}
		}
	}

	printf("%d\n", ret);

	return 0;
}


설정

트랙백

댓글

[삼성 SW 역량 테스트] 사다리 조작





#include <stdio.h>

int n, m, h, ret;
int map[31][11];

bool check() {
	bool ret = true;

	for (int i = 1; i <= n; ++i) {
		int pos = i;

		for (int j = 0; j <= h; ++j) {
			if (map[j][pos] == 1) {
				++pos;
			}
			else if (map[j][pos - 1] == 1) {
				--pos;
			}
		}

		if (pos != i) {
			return ret = false;
		}
	}

	return ret;
}

void dfs(int count, int y, int x) {
	if (count >= ret)	return;
	if (check()) {
		ret = count;
		return;
	}
	if (count == 3)	return;
	for (int i = y; i <= h; ++i) {
		for (int j = x; j < n; ++j) {
			if (map[i][j] == 0 && map[i][j - 1] == 0 && map[i][j + 1] == 0) {
				map[i][j] = 1;
				dfs(count + 1, i, j);
				map[i][j] = 0;
			}
		}
		x = 1;
	}
}

int main()
{
	scanf("%d %d %d", &n, &m, &h);
	int a, b;
	for (int i = 0; i < m; ++i) {
		scanf("%d %d", &a, &b);
		map[a][b] = 1;
	}

	ret = 4;
	dfs(0, 1, 1);
	if (ret == 4)	ret = -1;
	printf("%d\n", ret);

	return 0;
}


설정

트랙백

댓글