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


설정

트랙백

댓글

[삼성 SW 역량 테스트] 구슬탈출2







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

struct INFO {
	int ry, rx, by, bx, count;
};

INFO start;
char map[10][11];

int bfs() {
	const int dy[] = { -1, 1, 0, 0 };
	const int dx[] = { 0, 0, -1, 1 };

	int visited[10][10][10][10] = { 0, };
	queue<INFO> q;
	q.push(start);
	visited[start.ry][start.rx][start.by][start.bx] = 1;
	
	int ret = -1;
	while (!q.empty()) {
		INFO cur = q.front();	q.pop();
		if (cur.count > 10)	break;
		if (map[cur.ry][cur.rx] == 'O' && map[cur.by][cur.bx] != 'O') {
			ret = cur.count;
			break;
		}

		for (int dir = 0; dir < 4; ++dir) {
			int next_ry = cur.ry;
			int next_rx = cur.rx;
			int next_by = cur.by;
			int next_bx = cur.bx;

			while (1) {
				if (map[next_ry][next_rx] != '#' && map[next_ry][next_rx] != 'O') {
					next_ry += dy[dir], next_rx += dx[dir];
				}
				else {
					if (map[next_ry][next_rx] == '#') {
						next_ry -= dy[dir], next_rx -= dx[dir];
					}
					break;
				}
			}

			while (1) {
				if (map[next_by][next_bx] != '#' && map[next_by][next_bx] != 'O') {
					next_by += dy[dir], next_bx += dx[dir];
				}
				else {
					if (map[next_by][next_bx] == '#') {
						next_by -= dy[dir], next_bx -= dx[dir];
					}
					break;
				}
			}

			if (next_ry == next_by && next_rx == next_bx) {
				if (map[next_ry][next_rx] != 'O') {
					int red_dist = abs(next_ry - cur.ry) + abs(next_rx - cur.rx);
					int blue_dist = abs(next_by - cur.by) + abs(next_bx - cur.bx);
					if (red_dist > blue_dist) {
						next_ry -= dy[dir], next_rx -= dx[dir];
					}
					else {
						next_by -= dy[dir], next_bx -= dx[dir];
					}
				}
			}

			if (visited[next_ry][next_rx][next_by][next_bx] == 0) {
				visited[next_ry][next_rx][next_by][next_bx] = 1;
				INFO next;
				next.ry = next_ry;
				next.rx = next_rx;
				next.by = next_by;
				next.bx = next_bx;
				next.count = cur.count + 1;
				q.push(next);
			}
		}
	}
	return ret;
}

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

	for (int y = 0; y < n; ++y) {
		for (int x = 0; x < m; ++x) {
			if (map[y][x] == 'R') {
				start.ry = y, start.rx = x;
			}
			if (map[y][x] == 'B') {
				start.by = y, start.bx = x;
			}
		}
	}
	start.count = 0;

	int ret = bfs();
	printf("%d\n", ret);

	return 0;
}


설정

트랙백

댓글