[삼성 SW 역량 테스트] 주사위 굴리기





#include <stdio.h>

int n, m, sy, sx, k;
int map[20][20];

int dice[6];

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

void move_dice(int dir) {
	int ndice[6];
	switch (dir)
	{
	case 0:
		ndice[0] = dice[0];
		ndice[5] = dice[1];
		ndice[2] = dice[2];
		ndice[4] = dice[3];
		ndice[1] = dice[4];
		ndice[3] = dice[5];
		break;
	case 1:
		ndice[0] = dice[0];
		ndice[4] = dice[1];
		ndice[2] = dice[2];
		ndice[5] = dice[3];
		ndice[3] = dice[4];
		ndice[1] = dice[5];
		break;
	case 2:
		ndice[3] = dice[0];
		ndice[0] = dice[1];
		ndice[1] = dice[2];
		ndice[2] = dice[3];
		ndice[4] = dice[4];
		ndice[5] = dice[5];
		break;
	case 3:
		ndice[1] = dice[0];
		ndice[2] = dice[1];
		ndice[3] = dice[2];
		ndice[0] = dice[3];
		ndice[4] = dice[4];
		ndice[5] = dice[5];
		break;
	default:
		break;
	}
	for (int i = 0; i < 6; ++i) {
		dice[i] = ndice[i];
	}
}

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

	int dir;
	for (int i = 0; i < k; ++i) {
		scanf("%d", &dir);
		--dir;
		int ny = sy + dy[dir];
		int nx = sx + dx[dir];
		if (ny < 0 || ny >= n || nx < 0 || nx >= m) {
			continue;
		}

		move_dice(dir);

		if (map[ny][nx] == 0) {
			map[ny][nx] = dice[3];
		}
		else {
			dice[3] = map[ny][nx];
			map[ny][nx] = 0;
		}
		sy = ny;
		sx = nx;

		printf("%d\n", dice[1]);
	}

	return 0;
}


설정

트랙백

댓글

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





#include <stdio.h>

int n, b, c;
int p[1000000];

int main()
{
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &p[i]);
	}
	scanf("%d %d", &b, &c);

	long long ret = 0;
	for (int i = 0; i < n; ++i) {
		p[i] -= b;
		++ret;
		if (p[i] > 0) {
			int candi = p[i] / c;
			if (p[i] % c != 0) {
				++candi;
			}
			ret += candi;
		}
	}
	printf("%lld\n", ret);
	return 0;
}


설정

트랙백

댓글

[삼성 SW 역량 테스트] 뱀





#include <stdio.h>

int n, k, m;
int map[101][101];

int head_y, head_x, tail_index;
int snake_y[10101], snake_x[10101];
char cmd[10001];

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

int main()
{
	scanf("%d %d", &n, &k);

	int y, x;
	for (int i = 0; i < k; ++i) {
		scanf("%d %d", &y, &x);
		map[y][x] = 1;
	}

	int time;
	char c;
	scanf("%d", &m);
	for (int i = 0; i < m; ++i) {
		scanf("%d %c", &time, &c);
		cmd[time] = c;
	}

	int dir = 0;
	time = 0;
	head_y = 1, head_x = 1, tail_index = time;
	snake_y[time] = head_y, snake_x[time] = head_x;
	map[head_y][head_x] = -1;

	while (true) {
		++time;

		head_y += dy[dir];
		head_x += dx[dir];

		if (head_y < 1 || head_y > n || head_x < 1 || head_x > n || map[head_y][head_x] == -1) {
			break;
		}

		snake_y[time] = head_y;
		snake_x[time] = head_x;
		if (map[head_y][head_x] == 0) {
			int tail_y = snake_y[tail_index];
			int tail_x = snake_x[tail_index];
			map[tail_y][tail_x] = 0;
			++tail_index;
		}
		map[head_y][head_x] = -1;

		if (cmd[time] == 'D') {
			dir = (dir + 1) % 4;
		}

		if (cmd[time] == 'L') {
			dir = (dir + 3) % 4;
		}
	}

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


설정

트랙백

댓글

[삼성 SW 역량 테스트] 2048(Easy)





#include <stdio.h>

int n, ret;

struct BOARD {
	int map[20][20];

	void rotate() {
		int temp[20][20];

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

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

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

	void up() {
		int temp[20][20];

		for (int x = 0; x < n; ++x) {
			int flag = 0, target = -1;
			for (int y = 0; y < n; ++y) {
				if (map[y][x] == 0) {
					continue;
				}
				if (flag == 1 && map[y][x] == temp[target][x]) {
					temp[target][x] *= 2, flag = 0;
				}
				else {
					temp[++target][x] = map[y][x], flag = 1;
				}
			}
			for (++target; target < n; ++target) {
				temp[target][x] = 0;
			}
		}
		for (int y = 0; y < n; ++y) {
			for (int x = 0; x < n; ++x) {
				map[y][x] = temp[y][x];
			}
		}
	}
};

void dfs(BOARD cur, int count) {
	if (count == 5) {
		int candi = cur.get_max();
		if (ret < candi) {
			ret = candi;
		}
		return;
	}

	for (int dir = 0; dir < 4; ++dir) {
		BOARD next = cur;
		next.up();
		dfs(next, count + 1);
		cur.rotate();
	}
}

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

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


설정

트랙백

댓글