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


설정

트랙백

댓글

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


설정

트랙백

댓글