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


설정

트랙백

댓글

[삼성 SW 역량 테스트] 연구소





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

int n, m, ret;
int map[8][8];

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

void bfs() {
	queue<int> q;
	int backup[8][8];
	int visited[8][8] = { 0, };
	for (int y = 0; y < n; ++y) {
		for (int x = 0; x < m; ++x) {
			backup[y][x] = map[y][x];
			if (backup[y][x] == 2) {
				q.push(y * 10 + x);
				visited[y][x] = 1;
			}
		}
	}

	while (!q.empty()) {
		int cur = q.front();	q.pop();

		int cy = cur / 10;
		int cx = cur % 10;

		backup[cy][cx] = 2;

		for (int dir = 0; dir < 4; ++dir) {
			int ny = cy + dy[dir];
			int nx = cx + dx[dir];

			if (ny < 0 || ny >= n || nx < 0 || nx >= m) {
				continue;
			}

			if (visited[ny][nx] == 0 && backup[ny][nx] == 0) {
				visited[ny][nx] = 1;
				q.push(ny * 10 + nx);
			}
		}
	}

	int candi = 0;
	for (int y = 0; y < n; ++y) {
		for (int x = 0; x < m; ++x) {
			if (backup[y][x] == 0) {
				++candi;
			}
		}
	}

	if (ret < candi) {
		ret = candi;
	}
}

void pick_dfs(int count, int sy, int sx) {
	if (count == 3) {
		bfs();
		return;
	}

	for (int y = sy; y < n; ++y) {
		for (int x = sx; x < m; ++x) {
			if (map[y][x] == 0) {
				map[y][x] = 1;
				pick_dfs(count + 1, y, x);
				map[y][x] = 0;
			}
		}
		sx = 0;
	}
}

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]);
		}
	}

	pick_dfs(0, 0, 0);

	printf("%d\n", ret);

	return 0;
}


설정

트랙백

댓글

[삼성 SW 역량 테스트] 퇴사





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

int n;
int T[15], P[15];
int cache[15];

int solve(int day) {
	if (day > n)	return -987654321;
	if (day == n)	return 0;

	int& ret = cache[day];
	if (ret != -1)	return ret;

	ret = max(solve(day + 1), solve(day + T[day]) + P[day]);
	return ret;
}

int main()
{
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf("%d %d", &T[i], &P[i]);
		cache[i] = -1;
	}

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


설정

트랙백

댓글

[삼성 SW 역량 테스트] 테트로미노





#include <stdio.h>

int n, m;
int map[503][503];

const char block[19][4][5] = {
	{
		"1111",
		"0000",
		"0000",
		"0000",
	},
	{
		"1000",
		"1000",
		"1000",
		"1000",
	},
	{
		"1100",
		"1100",
		"0000",
		"0000",
	},
	{
		"1000",
		"1000",
		"1100",
		"0000",
	},
	{
		"1110",
		"1000",
		"0000",
		"0000",
	},
	{
		"1100",
		"0100",
		"0100",
		"0000",
	},
	{
		"0010",
		"1110",
		"0000",
		"0000",
	},
	{
		"0100",
		"0100",
		"1100",
		"0000",
	},
	{
		"1000",
		"1110",
		"0000",
		"0000",
	},
	{
		"1100",
		"1000",
		"1000",
		"0000",
	},
	{
		"1110",
		"0010",
		"0000",
		"0000",
	},
	{
		"1000",
		"1100",
		"0100",
		"0000",
	},
	{
		"0110",
		"1100",
		"0000",
		"0000",
	},
	{
		"0100",
		"1100",
		"1000",
		"0000",
	},
	{
		"1100",
		"0110",
		"0000",
		"0000",
	},
	{
		"1110",
		"0100",
		"0000",
		"0000",
	},
	{
		"0100",
		"1100",
		"0100",
		"0000",
	},
	{
		"0100",
		"1110",
		"0000",
		"0000",
	},
	{
		"1000",
		"1100",
		"1000",
		"0000",
	}
};

int get_count(int sy, int sx, int k) {
	int ret = 0;
	for (int y = 0; y < 4; ++y) {
		for (int x = 0; x < 4; ++x) {
			ret += (block[k][y][x] - '0') * map[sy + y][sx + x];
		}
	}
	return ret;
}

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]);
		}
	}

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

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

	int ret = 0;
	for (int y = 0; y < n; ++y) {
		for (int x = 0; x < m; ++x) {
			for (int k = 0; k < 19; ++k) {
				int candi = get_count(y, x, k);
				if (ret < candi) {
					ret = candi;
				}
			}
		}
	}
	printf("%d\n", ret);

	return 0;
}


설정

트랙백

댓글

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


설정

트랙백

댓글