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


설정

트랙백

댓글

[삼성 SW 역량 테스트] 연산자 끼워넣기





#include <stdio.h>

int n;
int number[11];
int op[4];
int ret_min = 0x7fffffff, ret_max = ret_min * -1;

void dfs(int result, int count) {
	if (count == n - 1) {
		if (ret_min > result) {
			ret_min = result;
		}
		if (ret_max < result) {
			ret_max = result;
		}
		return;
	}

	for (int i = 0; i < 4; ++i) {
		if (op[i] != 0) {
			--op[i];
			if (i == 0) {
				dfs(result + number[count + 1], count + 1);
			}
			else if (i == 1) {
				dfs(result - number[count + 1], count + 1);
			}
			else if (i == 2) {
				dfs(result * number[count + 1], count + 1);
			}
			else if (i == 3) {
				dfs(result / number[count + 1], count + 1);
			}
			++op[i];
		}
	}
}

int main()
{
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &number[i]);
	}
	for (int i = 0; i < 4; ++i) {
		scanf("%d", &op[i]);
	}

	dfs(number[0], 0);

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


설정

트랙백

댓글

[삼성 SW 역량 테스트] 로봇 청소기





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

struct ROBOT {
	int y, x, dir;
};

int n, m;
int map[50][50];
ROBOT robot;

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


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

	queue<robot> q;
	q.push(robot);

	int ret = 0;
	while (!q.empty()) {
		ROBOT cur = q.front();	q.pop();
		if (map[cur.y][cur.x] == 0) {
			map[cur.y][cur.x] = 2;
			++ret;
		}

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

			if (next.y < 0 || next.y >= n || next.x < 0 || next.x >= m
				|| map[next.y][next.x] != 0) {
				continue;
			}

			q.push(next);
			break;
		}

		if (q.empty()) {
			ROBOT next;
			next.dir = cur.dir;
			next.y = cur.y + dy[(next.dir + 2) % 4];
			next.x = cur.x + dx[(next.dir + 2) % 4];

			if (next.y < 0 || next.y >= n || next.x < 0 || next.x >= m
				|| map[next.y][next.x] == 1) {
				break;
			}
			q.push(next);
		}
	}

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


설정

트랙백

댓글