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


설정

트랙백

댓글

[삼성 SW 역량 테스트] 톱니 바퀴





#include <stdio.h>

int main()
{
	char gear[4][9];
	for (int i = 0; i < 4; ++i) {
		scanf("%s", gear[i]);
	}
	int k;
	scanf("%d", &k);
	while (k--) {
		int target, cmd;
		scanf("%d %d", &target, &cmd);
		--target;
		int move_cmd[4] = { 0, };
		move_cmd[target] = cmd;
		for (int left = target - 1; left >= 0; --left) {
			int right = left + 1;
			if (gear[left][2] == gear[right][6]) {
				break;
			}
			else {
				move_cmd[left] = move_cmd[right] * -1;
			}
		}
		for (int right = target + 1; right < 4; ++right) {
			int left = right - 1;
			if (gear[left][2] == gear[right][6]) {
				break;
			}
			else {
				move_cmd[right] = move_cmd[left] * -1;
			}
		}

		for (int i = 0; i < 4; ++i) {
			if (move_cmd[i] == 1) {
				char temp = gear[i][7];
				for (int j = 7; j >= 1; --j) {
					gear[i][j] = gear[i][j - 1];
				}
				gear[i][0] = temp;
			}
			else if (move_cmd[i] == -1) {
				char temp = gear[i][0];
				for (int j = 0; j < 7; ++j) {
					gear[i][j] = gear[i][j + 1];
				}
				gear[i][7] = temp;
			}
		}

	}
	
	int ret = 0;
	for (int i = 0; i < 4; ++i) {
		if (gear[i][0] == '1') {
			ret += (1 << i);
		}
	}
	printf("%d\n", ret);
	return 0;
}


설정

트랙백

댓글

[삼성 SW 역량 테스트] 경사로





#include <stdio.h>

int n, l, ret = 0;
int map[200][100];

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

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

	int count = 0;
	int i, j;

	ret = 0;
	for (i = 0; i < 2 * n; ++i) {
		count = 1;
		for (j = 0; j < n - 1; ++j) {
			if (map[i][j] == map[i][j + 1]) {
				++count;
			}
			else if (map[i][j] + 1 == map[i][j + 1] && count >= l) {
				count = 1;
			}
			else if (map[i][j] - 1 == map[i][j + 1] && count >= 0) {
				count = (1 - l);
			}
			else {
				break;
			}
		}
		if (j == (n - 1) && count >= 0) {
			++ret;
		}
	}
	printf("%d\n", ret);

	return 0;
}


설정

트랙백

댓글

[삼성 SW 역량 테스트] 스타트와 링크





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

int n, ret;
int table[20][20];
int team1[10], team2[10];
int pick[20];

void update() {
	int team1_size = 0, team2_size = 0;
	for (int i = 0; i < n; ++i) {
		if (pick[i] == 0) {
			team1[team1_size++] = i;
		}
		else {
			team2[team2_size++] = i;
		}
	}

	int sum_1 = 0, sum_2 = 0;
	for (int i = 0; i < n / 2; ++i) {
		for (int j = i + 1; j < n / 2; ++j) {
			sum_1 += (table[team1[i]][team1[j]] + table[team1[j]][team1[i]]);
			sum_2 += (table[team2[i]][team2[j]] + table[team2[j]][team2[i]]);
		}
	}
	if (ret > abs(sum_1 - sum_2)) {
		ret = abs(sum_1 - sum_2);
	}
}

void dfs(int cur, int pick_count) {
	if (pick_count == (n / 2)) {
		update();
		return;
	}

	for (int i = cur; i < n; ++i) {
		pick[i] = 1;
		dfs(i + 1, pick_count + 1);
		pick[i] = 0;
	}
}

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

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

	return 0;
}


설정

트랙백

댓글