[삼성 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;
}


설정

트랙백

댓글

[삼성 SW 역량 테스트] 감시





#include <stdio.h>

struct CCTV {
	int type, y, x;
};

int n, m, ret;
int map[8][8];
int cctv_size;
CCTV cctv[8];

const int rot_size[] = { 4, 2, 4, 4, 1 };

void map_copy(int desc[8][8], int src[8][8]) {
	for (int y = 0; y < n; ++y) {
		for (int x = 0; x < m; ++x) {
			desc[y][x] = src[y][x];
		}
	}
}

void update(int dir, CCTV cctv) {
	dir = (dir % 4);

	if (dir == 0) {
		for (int x = cctv.x + 1; x < m; ++x) {
			if (map[cctv.y][x] == 6)	break;
			map[cctv.y][x] = -1;
		}
	}
	if (dir == 1) {
		for (int y = cctv.y - 1; y >= 0; --y) {
			if (map[y][cctv.x] == 6)	break;
			map[y][cctv.x] = -1;
		}
	}
	if (dir == 2) {
		for (int x = cctv.x - 1; x >= 0; --x) {
			if (map[cctv.y][x] == 6)	break;
			map[cctv.y][x] = -1;
		}
	}
	if (dir == 3) {
		for (int y = cctv.y + 1; y < n; ++y) {
			if (map[y][cctv.x] == 6)	break;
			map[y][cctv.x] = -1;
		}
	}
}

void dfs(int cctv_index) {
	if (cctv_index == cctv_size) {
		int candi = 0;
		for (int y = 0; y < n; ++y) {
			for (int x = 0; x < m; ++x) {
				if (map[y][x] == 0) {
					++candi;
				}
			}
		}

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

	int backup[8][8];
	int type = cctv[cctv_index].type;
	for (int dir = 0; dir < rot_size[type]; ++dir) {
		map_copy(backup, map);
		if (type == 0) {
			update(dir, cctv[cctv_index]);
		}
		if (type == 1) {
			update(dir, cctv[cctv_index]);
			update(dir + 2, cctv[cctv_index]);
		}
		if (type == 2) {
			update(dir, cctv[cctv_index]);
			update(dir + 1, cctv[cctv_index]);
		}
		if (type == 3) {
			update(dir, cctv[cctv_index]);
			update(dir + 1, cctv[cctv_index]);
			update(dir + 2, cctv[cctv_index]);
		}
		if (type == 4) {
			update(dir, cctv[cctv_index]);
			update(dir + 1, cctv[cctv_index]);
			update(dir + 2, cctv[cctv_index]);
			update(dir + 3, cctv[cctv_index]);
		}
		dfs(cctv_index + 1);
		map_copy(map, backup);
	}
}

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

			if (map[y][x] != 0 && map[y][x] != 6) {
				cctv[cctv_size].y = y;
				cctv[cctv_size].x = x;
				cctv[cctv_size].type = map[y][x] - 1;
				++cctv_size;
			}
		}
	}

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

	return 0;
}


설정

트랙백

댓글