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


설정

트랙백

댓글

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


설정

트랙백

댓글