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

 

 

 

 

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

struct POS {
	int y, x, time;
};

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

int pos_size;
POS pos[10];

int bfs(int pick[]) {
	int empty_count = 0;
	for (int y = 0; y < n; ++y) {
		for (int x = 0; x < n; ++x) {
			if (map[y][x] == 0) {
				++empty_count;
			}
		}
	}

	queue<POS> q;
	int visited[50][50] = { 0, };
	for (int i = 0; i < m; ++i) {
		q.push(pos[pick[i]]);
		visited[pos[pick[i]].y][pos[pick[i]].x] = 1;
	}

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

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

		if (map[cur.y][cur.x] == 0) {
			--empty_count;
		}
		if (empty_count == 0) {
			return cur.time;
		}

		POS next;
		next.time = cur.time + 1;
		for (int d = 0; d < 4; ++d) {
			next.y = cur.y + dy[d];
			next.x = cur.x + dx[d];
			if (next.y < 0 || next.y >= n || next.x < 0 || next.x >= n) {
				continue;
			}

			if (visited[next.y][next.x] == 0 && map[next.y][next.x] != 1) {
				q.push(next);
				visited[next.y][next.x] = 1;
			}
		}
	}
	return 0x7fffffff;
}

void dfs(int last_pick, int pick_count, int pick[]) {
	if(pick_count == m) {
		int candi = bfs(pick);
		if (ret > candi) {
			ret = candi;
		}
		return;
	}

	for (int i = last_pick + 1; i < pos_size; ++i) {
		pick[pick_count] = i;
		dfs(i, pick_count + 1, pick);
	}
}

int main()
{	
	scanf("%d %d", &n, &m);
	for (int y = 0; y < n; ++y) {
		for(int x = 0; x < n; ++x) {
			scanf("%d", &map[y][x]);
			if (map[y][x] == 2) {
				pos[pos_size].y = y;
				pos[pos_size].x = x;
				pos[pos_size].time = 0;
				++pos_size;
			}
		}
	}
	ret = 0x7fffffff;
	int pick[10] = { 0, };
	dfs(-1, 0, pick);
	if (ret == 0x7fffffff) {
		printf("-1\n");
	}
	else {
		printf("%d\n", ret);
	}
	return 0;
}

설정

트랙백

댓글

[삼성 SW 역량 테스트] 이차원 배열과 연산

 

 

 

 

 

 

 

 

#include <stdio.h>

int r, c, k;
int ret;

int map[101][101];
int pre_row_size, cur_row_size;
int pre_col_size, cur_col_size;

void update_row(int sy) {
	int bucket[101] = { 0, };
	for (int x = 1; x <= pre_col_size; ++x) {
		++bucket[map[sy][x]];
		// bucket[3] = 5 라는 의미는.
		// map[sy] row에 3이라는 값이 5번 반복되었다.
	}

	int size = 0;
	for (int i = 1; i <= 100 && size < 100; ++i) {
		for (int j = 1; j <= 100 && size < 100; ++j) {
			if (bucket[j] == i) {
				map[sy][++size] = j;
				map[sy][++size] = i;
			}
		}
	}

	for (int i = size + 1; i <= pre_col_size; ++i) {
		map[sy][i] = 0;
	}

	if (cur_col_size < size) {
		cur_col_size = size;
	}
}

void update_col(int sx) {
	int bucket[101] = { 0, };
	for (int y = 1; y <= pre_row_size; ++y) {
		++bucket[map[y][sx]];
	}

	int size = 0;
	for (int i = 1; i <= 100 && size < 100; ++i) {
		for (int j = 1; j <= 100 && size < 100; ++j) {
			if (bucket[j] == i) {
				map[++size][sx] = j;
				map[++size][sx] = i;
			}
		}
	}

	for (int i = size + 1; i <= pre_row_size; ++i) {
		map[i][sx] = 0;
	}

	if (cur_row_size < size) {
		cur_row_size = size;
	}
}

void solve() {
	while (map[r][c] != k) {
		if (cur_row_size >= cur_col_size) {
			pre_col_size = cur_col_size;
			cur_col_size = 0;
			for (int y = 1; y <= cur_row_size; ++y) {
				update_row(y);
			}
		}
		else {
			pre_row_size = cur_row_size;
			cur_row_size = 0;
			for (int x = 1; x <= cur_col_size; ++x) {
				update_col(x);
			}
		}
		++ret;
		if (ret > 100) {
			break;
		}
	}
}

int main()
{
	pre_row_size = 3, cur_row_size = 3;
	pre_col_size = 3, cur_col_size = 3;
	scanf("%d %d %d", &r, &c, &k);

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

	ret = 0;
	solve();
	if (ret > 100) {
		printf("-1\n");
	}
	else {
		printf("%d\n", ret);
	}

	return 0;
}

설정

트랙백

댓글

[삼성 SW 역량 테스트] 낚시왕

 

 

 

 

 

 

 

#include <stdio.h>

struct CELL {
	int s, d, z;
};

int r, c, m;
CELL map[2][100][100];

int fishing(int cur, int pos) {
	int size = 0;
	for (int y = 0; y < r; ++y) {
		if (map[cur][y][pos].z != 0) {
			size = map[cur][y][pos].z;
			map[cur][y][pos].s = 0;
			map[cur][y][pos].d = 0;
			map[cur][y][pos].z = 0;
			break;
		}
	}
	return size;
}

// 1 -> up
// 2 -> down
// 3 -> right
// 4 -> down
void move(int cur) {
	int next = (cur + 1) % 2;
	for (int y = 0; y < r; ++y) {
		for (int x = 0; x < c; ++x) {
			map[next][y][x].s = 0;
			map[next][y][x].d = 0;
			map[next][y][x].z = 0;
		}
	}

	for (int y = 0; y < r; ++y) {
		for (int x = 0; x < c; ++x) {
			if (map[cur][y][x].z != 0) {
				//y 감소
				if (map[cur][y][x].d == 1) {
					int ny = (((r - 1) * 2) - y) + map[cur][y][x].s;
					ny = (ny % ((r - 1) * 2));
					int nd = 2;
					if (ny >= (r - 1)) {
						ny = ((r - 1) * 2) - ny;
						nd = 1;
					}
					if (map[next][ny][x].z < map[cur][y][x].z) {
						map[next][ny][x].s = map[cur][y][x].s;
						map[next][ny][x].d = nd;
						map[next][ny][x].z = map[cur][y][x].z;
					}
				}
				//y 증가
				else if (map[cur][y][x].d == 2) {
					int ny = y + map[cur][y][x].s;
					ny = (ny % ((r - 1) * 2));
					int nd = 2;
					if (ny >= (r - 1)) {
						ny = ((r - 1) * 2) - ny;
						nd = 1;
					}
					if (map[next][ny][x].z < map[cur][y][x].z) {
						map[next][ny][x].s = map[cur][y][x].s;
						map[next][ny][x].d = nd;
						map[next][ny][x].z = map[cur][y][x].z;
					}
				}
				//x 증가
				else if (map[cur][y][x].d == 3) {
					int nx = x + map[cur][y][x].s;
					nx = (nx % ((c - 1) * 2));
					int nd = 3;
					if (nx >= (c - 1)) {
						nx = ((c - 1) * 2) - nx;
						nd = 4;
					}
					if (map[next][y][nx].z < map[cur][y][x].z) {
						map[next][y][nx].s = map[cur][y][x].s;
						map[next][y][nx].d = nd;
						map[next][y][nx].z = map[cur][y][x].z;
					}
				}
				//x 감소
				else if (map[cur][y][x].d == 4) {
					int nx = (((c - 1) * 2) - x) + map[cur][y][x].s;
					nx = (nx % ((c - 1) * 2));
					int nd = 3;
					if (nx >= (c - 1)) {
						nx = ((c - 1) * 2) - nx;
						nd = 4;
					}
					if (map[next][y][nx].z < map[cur][y][x].z) {
						map[next][y][nx].s = map[cur][y][x].s;
						map[next][y][nx].d = nd;
						map[next][y][nx].z = map[cur][y][x].z;
					}
				}
			}
		}
	}
}

int main()
{
	scanf("%d %d %d", &r, &c, &m);
	int y, x, cur = 0;
	for (int i = 0; i < m; ++i) {
		scanf("%d %d", &y, &x);
		--y, --x;
		scanf("%d %d %d", &map[cur][y][x].s, &map[cur][y][x].d, &map[cur][y][x].z);
	}

	int ret = 0;
	for (int pos = 0; pos < c; ++pos) {
		ret += fishing(cur, pos);
		move(cur);
		cur = (cur + 1) % 2;
	}
	printf("%d\n", ret);

	return 0;
}

설정

트랙백

댓글

[삼성 SW 역량 테스트] 미세먼지 안녕!

 

 

 

 

#include <stdio.h>

int r, c, t;

int map[2][50][50];

int up_y, up_x, down_y, down_x;

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

void spead(int cur) {
	int next = (cur + 1) % 2;
	for (int y = 0; y < r; ++y) {
		for(int x = 0; x < c; ++x) {
			if (map[cur][y][x] == -1) {
				map[next][y][x] = -1;
			}
			else {
				map[next][y][x] = 0;
			}
		}
	}

	for (int y = 0; y < r; ++y) {
		for (int x = 0; x < c; ++x) {
			int val = (map[cur][y][x] / 5);
			int candi = 0;
			for (int dir = 0; dir < 4; ++dir) {
				int ny = y + dy[dir];
				int nx = x + dx[dir];

				if (ny < 0 || ny >= r || nx < 0 || nx >= c) {
					continue;
				}

				if(map[next][ny][nx] != -1) {
					map[next][ny][nx] += val;
					candi += val;
				}
			}

			if (map[next][y][x] != -1) {
				map[next][y][x] += (map[cur][y][x] - candi);
			}
		}
	}
}

void move(int cur) {
	for (int y = up_y - 1; y > 0; --y) {
		map[cur][y][0] = map[cur][y - 1][0];
	}

	for (int x = 0; x < c - 1; ++x) {
		map[cur][0][x] = map[cur][0][x + 1];
	}

	for (int y = 0; y < up_y; ++y) {
		map[cur][y][c - 1] = map[cur][y + 1][c - 1];
	}

	for (int x = c - 1; x > 1; --x) {
		map[cur][up_y][x] = map[cur][up_y][x - 1];
	}
	
	map[cur][up_y][1] = 0;


	for (int y = down_y + 1; y < r; ++y) {
		map[cur][y][0] = map[cur][y + 1][0];
	}

	for (int x = 0; x < c - 1; ++x) {
		map[cur][r - 1][x] = map[cur][r - 1][x + 1];
	}

	for (int y = r - 1; y > down_y; --y) {
		map[cur][y][c - 1] = map[cur][y - 1][c - 1];
	}

	for (int x = c - 1; x > 1; --x) {
		map[cur][down_y][x] = map[cur][down_y][x - 1];
	}

	map[cur][down_y][1] = 0;

}

int main()
{
	up_y = -1;
	scanf("%d %d %d", &r, &c, &t);
	for (int y = 0; y < r; ++y) {
		for (int x = 0; x < c; ++x) {
			scanf("%d", &map[0][y][x]);
			if (map[0][y][x] == -1) {
				if (up_y == -1) {
					up_y = y, up_x = x;
				}
				else {
					down_y = y, down_x = x;
				}
			}
		}
	}

	int cur = 0;
	for (int time = 0; time < t; ++time) {
		spead(cur);
		cur = (cur + 1) % 2;
		move(cur);
	}

	int ret = 0;
	for (int y = 0; y < r; ++y) {
		for (int x = 0; x < c; ++x) {
			if (map[cur][y][x] != -1) {
				ret += map[cur][y][x];
			}
		}
	}

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

	return 0;
}

설정

트랙백

댓글

Linked List

Data Structure 2019. 2. 13. 23:00


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

struct NODE {
	int prev;
	int next;
	int val;
};

const int NODE_SIZE = 30000;

//TEST CMD
const int PUSH_BACK = 0;
const int PUSH_FRONT = 1;
const int INSERT = 2;
const int POP_BACK = 3;
const int POP_FRONT = 4;
const int ERASE = 5;

int test_cmd[NODE_SIZE][3];

struct MY_LIST {
	int HEAD = NODE_SIZE;
	int TAIL = NODE_SIZE + 1;
	int pos;
	NODE node[NODE_SIZE + 2];

	MY_LIST() {
		pos = 0;
		node[HEAD].next = TAIL;
		node[TAIL].prev = HEAD;
	}

	void push_back(int data) {
		int prev = node[TAIL].prev;
		int next = node[prev].next;	// TAIL;

		node[pos].val = data;

		node[pos].prev = prev;
		node[prev].next = pos;

		node[pos].next = next;
		node[next].prev = pos;
		++pos;
	}

	void push_front(int data) {
		int next = node[HEAD].next;
		int prev = node[next].prev;	// HEAD
		
		node[pos].val = data;

		node[pos].prev = prev;
		node[prev].next = pos;

		node[pos].next = next;
		node[next].prev = pos;
		++pos;
	}

	void insert(int p, int data) {
		int next = node[HEAD].next;
		for(int i = 0; i < p; ++i) {
			next = node[next].next;
		}
		int prev = node[next].prev;
	
		node[pos].val = data;

		node[pos].prev = prev;
		node[prev].next = pos;

		node[pos].next = next;
		node[next].prev = pos;
		++pos;
	}

	void pop_back() {
		int target = node[TAIL].prev;

		int prev = node[target].prev;
		int next = node[target].next;

		node[prev].next = next;
		node[next].prev = prev;
	}

	void pop_front() {
		int target = node[HEAD].next;

		int prev = node[target].prev;
		int next = node[target].next;

		node[prev].next = next;
		node[next].prev = prev;
	}

	void erase(int p) {
		int target = node[HEAD].next;
		for (int i = 0; i < p; ++i) {
			target = node[target].next;
		}
		int prev = node[target].prev;
		int next = node[target].next;

		node[prev].next = next;
		node[next].prev = prev;
	}
};

MY_LIST my_list;
list<int> stl_list;

int main()
{
	// make test case..
	int cur_size = 0;
	for (int i = 0; i < NODE_SIZE; ++i) {
		if (i < NODE_SIZE / 3) {
			test_cmd[i][0] = rand() % 2;
		}
		else {
			test_cmd[i][0] = rand() % 6;
		}

		switch (test_cmd[i][0]) {
		case PUSH_BACK:
		case PUSH_FRONT: {
			test_cmd[i][1] = rand();
			++cur_size;
			break;
		}
		case INSERT: {
			test_cmd[i][1] = rand() % cur_size;
			test_cmd[i][2] = rand();
			++cur_size;
			break;
		}
		case POP_BACK:
		case POP_FRONT: {
			--cur_size;
			break;
		}
		case ERASE: {
			test_cmd[i][1] = rand() % cur_size;
			--cur_size;
			break;
		}
		}
	}

	// test my list
	int my_list_begin = GetTickCount();
	for (int i = 0; i < NODE_SIZE; ++i) {
		switch (test_cmd[i][0]) {
		case PUSH_BACK: {
			my_list.push_back(test_cmd[i][1]);
			break;
		}
		case PUSH_FRONT: {
			my_list.push_front(test_cmd[i][1]);
			break;
		}
		case INSERT: {
			my_list.insert(test_cmd[i][1], test_cmd[i][2]);
			break;
		}

		case POP_BACK: {
			my_list.pop_back();
			break;
		}
		case POP_FRONT: {
			my_list.pop_front();
			break;
		}
		case ERASE: {
			my_list.erase(test_cmd[i][1]);
			break;
		}
		}
	}
	int my_list_end = GetTickCount();

	// test stl list
	int stl_list_begin = GetTickCount();
	for (int i = 0; i < NODE_SIZE; ++i) {
		switch (test_cmd[i][0]) {
		case PUSH_BACK: {
			stl_list.push_back(test_cmd[i][1]);
			break;
		}
		case PUSH_FRONT: {
			stl_list.push_front(test_cmd[i][1]);
			break;
		}
		case INSERT: {
			list<int>::iterator it = stl_list.begin();
			for (int k = 0; k < test_cmd[i][1]; ++k) {
				++it;
			}
			stl_list.insert(it, test_cmd[i][2]);
			break;
		}

		case POP_BACK: {
			stl_list.pop_back();
			break;
		}
		case POP_FRONT: {
			stl_list.pop_front();
			break;
		}
		case ERASE: {
			list<int>::iterator it = stl_list.begin();
			for (int k = 0; k < test_cmd[i][1]; ++k) {
				++it;
			}
			stl_list.erase(it);
			break;
		}
		}
	}
	int stl_list_end = GetTickCount();

	//time compare
	printf("my list : %d\n", (my_list_end - my_list_begin));
	printf("stl list : %d\n", (stl_list_end - stl_list_begin));

	//result test
	list<int>::iterator it = stl_list.begin();
	int cur = my_list.node[my_list.HEAD].next;
	while (it != stl_list.end()) {
		if (*it != my_list.node[cur].val) {
			printf("Error\n");
		}
		++it;
		cur = my_list.node[cur].next;
	}

	return 0;
}


설정

트랙백

댓글