[삼성 SW 역량 테스트] 마법사 상어와 파이어볼

 

마법사 상어와 파이어볼 강의

 

마법사 상어와 파이어볼 코드

 

 

 

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

struct FIREBALL {
	int y, x;
	int m, s, d;
};

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

int N, M, K;
vector<FIREBALL> ball;
vector<int> map[50][50];

void move() {
	vector<int> new_map[50][50];
	for (int i = 0; i < ball.size(); ++i) {
		int dir = ball[i].d;
		int speed = (ball[i].s % N);
		int ny = (ball[i].y + (dy[dir] * speed) + N) % N;
		int nx = (ball[i].x + (dx[dir] * speed) + N) % N;
		new_map[ny][nx].push_back(i);
		ball[i].y = ny;
		ball[i].x = nx;
	}

	for (int y = 0; y < N; ++y) {
		for (int x = 0; x < N; ++x) {
			map[y][x] = new_map[y][x];
		}
	}
}

void sum() {
	vector<FIREBALL> new_ball;
	for (int y = 0; y < N; ++y) {
		for (int x = 0; x < N; ++x) {
			if (map[y][x].size() == 0) {
				continue;
			}
			if (map[y][x].size() == 1) {
				int index = map[y][x][0];
				new_ball.push_back(ball[index]);
				continue;
			}
			int sum_m = 0, sum_s = 0;
			bool odd = true, even = true;
			for (int i = 0; i < map[y][x].size(); ++i) {
				int index = map[y][x][i];
				sum_m += ball[index].m;
				sum_s += ball[index].s;
				if (ball[index].d % 2 == 0) {
					odd = false;
				}
				else {
					even = false;
				}
			}

			if (sum_m / 5 == 0) {
				continue;
			}
			int cur_m = sum_m / 5;
			int cur_s = sum_s / map[y][x].size();
			for (int i = 0; i < 4; ++i) {
				if (odd || even) {
					new_ball.push_back({ y, x, cur_m, cur_s, i * 2 });
				}
				else {
					new_ball.push_back({ y, x, cur_m, cur_s, i * 2 + 1 });
				}
			}
		}
	}

	ball = new_ball;
}

int solve() {
	while(K--) {
		move();
		sum();
	}
	int ret = 0;
	for (int i = 0; i < ball.size(); ++i) {
		ret += ball[i].m;
	}
	return ret;
}

int main()
{
	scanf("%d %d %d", &N, &M, &K);
	for (int i = 0; i < M; ++i) {
		int y, x, m, s, d;
		scanf("%d %d %d %d %d", &y, &x, &m, &s, &d);
		--y, --x;
		ball.push_back({ y, x, m, s, d });
		map[y][x].push_back(i);
	}
	int ret = solve();
	printf("%d\n", ret);
	return 0;
}

설정

트랙백

댓글

[삼성 SW 역량 테스트] 컨베이어 벨트 위의 로봇

 

컨베이어 벨트 위의 로봇 강의

 

컨베이어 벨트 위의 로봇 코드

 

 

#include <stdio.h>

int N, K; 
int A[200];

int solve() {
	int ret = 0;
	int zero_count = 0;
	int robot[200 * 1000];
	int front = 0, back = 0;

	while (zero_count < K) {
		++ret;

		int temp = A[2 * N - 1];
		for (int i = 2 * N - 1; i >= 1; --i) {
			A[i] = A[i - 1];
		}
		A[0] = temp;
		for (int i = front; i < back; ++i) {
			++robot[i];
			if (robot[i] == N - 1) {
				++front;
			}
		}

		for (int i = front; i < back; ++i) {
			int next = robot[i] + 1;
			if (A[next] == 0 || (i != front && robot[i - 1] == next)) {
				continue;
			}
			robot[i] = next;
			if (robot[i] == N - 1) {
				++front;
			}
			--A[next];
			if (A[next] == 0) {
				++zero_count;
			}
		}

		if (A[0] > 0 && (back == 0 || robot[back - 1] != 0)) {
			robot[back++] = 0;
			--A[0];
			if (A[0] == 0) {
				++zero_count;
			}
		}
	}

	return ret;
}

int main()
{
	scanf("%d %d", &N, &K);
	for (int i = 0; i < N; ++i) {
		scanf("%d %d", &A[i * 2], &A[i * 2 + 1]);
	}
	int ret = solve();
	printf("%d\n", ret);
	return 0;
}

설정

트랙백

댓글

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

 

 

 

 

 

 

 

 

 

 

 

#include <stdio.h>

const int BLUE = 0;
const int GREEN = 1;

int N, score, count;
int board[2][10][4];

void move_point(int y, int x, int color) {
	int label = board[color][y][x];
	board[color][y][x] = 0;
	while (y < 10) {
		if (board[color][y][x] != 0) {
			break;
		}
		++y;
	}
	--y;
	board[color][y][x] = label;
}
void move_h_block(int y, int x, int color) {
	int label = board[color][y][x];
	board[color][y][x] = 0;
	board[color][y - 1][x] = 0;
	while (y < 10) {
		if (board[color][y][x] != 0) {
			break;
		}
		++y;
	}
	--y;
	board[color][y][x] = label;
	board[color][y - 1][x] = label;
}
void move_w_block(int y, int x, int color) {
	int label = board[color][y][x];
	board[color][y][x] = 0;
	board[color][y][x + 1] = 0;
	while (y < 10) {
		if (board[color][y][x] != 0 || board[color][y][x + 1] != 0) {
			break;
		}
		++y;
	}
	--y;
	board[color][y][x] = label;
	board[color][y][x + 1] = label;
}

void remove(int y, int color) {
	for (int x = 0; x < 4; ++x) {
		board[color][y][x] = 0;
	}
}

void move(int start_y, int color) {
	const int dy[2] = { -1, 0 };
	const int dx[2] = { 0, +1 };
	for (int y = start_y; y >= 4; --y) {
		for (int x = 0; x < 4; ++x) {
			if (board[color][y][x] == 0) {
				continue;
			}
			//type 1 one block
			//type 2 h block
			//tyep 3 w block
			int type = 1;
			for (int d = 0; d < 2; ++d) {
				int ny = y + dy[d];
				int nx = x + dx[d];
				if (ny < 4 || nx >= 4) {
					continue;
				}
				if (board[color][y][x] == board[color][ny][nx]) {
					if (d == 0) {
						type = 2;
					}
					else {
						type = 3;
					}
				}
			}
			if (type == 1) {
				move_point(y, x, color);
			}
			else if (type == 2) {
				move_h_block(y, x, color);
			}
			else if (type == 3) {
				move_w_block(y, x, color);
			}
		}
	}
}

void delete_filled_block(int color) {
	bool is_remove = false;
	for (int y = 6; y < 10; ++y) {
		int count = 0;
		for (int x = 0; x < 4; ++x) {
			if (board[color][y][x] != 0) {
				++count;
			}
		}
		if (count == 4) {
			is_remove = true;
			++score;
			remove(y, color);
			move(y - 1, color);
		}
	}

	if (is_remove) {
		delete_filled_block(color);
	}
}
void delete_overflow_block(int color) {
	int remove_count = 0;
	for (int y = 4; y <= 5; ++y) {
		bool has_block = false;
		for (int x = 0; x < 4; ++x) {
			if (board[color][y][x] != 0) {
				has_block = true;
				break;
			}
		}
		if (has_block) {
			++remove_count;
		}
	}

	if (remove_count > 0) {
		for (int y = 9; y >= 6; --y) {
			for (int x = 0; x < 4; ++x) {
				board[color][y][x] = board[color][y - remove_count][x];
			}
		}
		for (int y = 4; y <= 5; ++y) {
			for (int x = 0; x < 4; ++x) {
				board[color][y][x] = 0;
			}
		}
	}
}

void put(int type, int target, int color, int label) {
	if (type == 1) {
		board[color][0][target] = label;
		move_point(0, target, color);
	}
	else if ((type == 2 && color == BLUE) || (type == 3 && color == GREEN)) {
		board[color][0][target] = label;
		board[color][1][target] = label;
		move_h_block(1, target, color);
	}
	else if ((type == 3 && color == BLUE) || (type == 2 && color == GREEN)) {
		board[color][0][target] = label;
		board[color][0][target + 1] = label;
		move_w_block(0, target, color);
	}
	delete_filled_block(color);
	delete_overflow_block(color);
}

int main()
{
	score = 0, count = 0;
	scanf("%d", &N);
	for (int i = 0; i < N; ++i) {
		int t, x, y;
		scanf("%d %d %d", &t, &y, &x);
		put(t, y, BLUE, i + 1);
		put(t, x, GREEN, i + 1);
	}
	for (int color = 0; color < 2; ++color) {
		for (int y = 4; y < 10; ++y) {
			for (int x = 0; x < 4; ++x) {
				if (board[color][y][x] != 0) {
					++count;
				}
			}
		}
	}

	printf("%d\n%d\n", score, count);

	return 0;
}

설정

트랙백

댓글

[삼성 SW 역량 테스트] 원판 돌리기

 

 

 

 

 

#include <stdio.h>

const int DEL = -1;
int N, M, T;
int board[50][50];

void solve(int x, int d, int k) {
	int pick = x - 1;
	if (d == 1) {
		k = -k;
	}
	while (pick < N) {
		int temp[50] = { 0, };
		for (int i = 0; i < M; ++i) {
			temp[(i + k + M) % M] = board[pick][i];
		}

		for (int i = 0; i < M; ++i) {
			board[pick][i] = temp[i];
		}
		pick += x;
	}

	bool is_update = false;
	const int dy[4] = { -1, +1, 0, 0 };
	const int dx[4] = { 0, 0, -1, +1 };
	bool check[50][50] = { false, };

	for (int y = 0; y < N; ++y) {
		for (int x = 0; x < M; ++x) {
			for (int d = 0; d < 4; ++d) {
				int ny = y + dy[d];
				int nx = (x + dx[d] + M) % M;
				if (ny < 0 || ny >= N) {
					continue;
				}
				if (board[y][x] != DEL && board[ny][nx] != DEL && board[y][x] == board[ny][nx]) {
					is_update = true;
					check[y][x] = true;
					check[ny][nx] = true;
				}

			}
		}
	}

	if (is_update) {
		for (int y = 0; y < N; ++y) {
			for (int x = 0; x < M; ++x) {
				if (check[y][x]) {
					board[y][x] = DEL;
				}
			}
		}
	}
	else {
		int sum = 0, count = 0;
		for (int y = 0; y < N; ++y) {
			for (int x = 0; x < M; ++x) {
				if (board[y][x] != DEL) {
					sum += board[y][x];
					++count;
				}
			}
		}
		for (int y = 0; y < N; ++y) {
			for (int x = 0; x < M; ++x) {
				if (board[y][x] != DEL) {
					if (board[y][x] * count > sum) {
						--board[y][x];
					}
					else if (board[y][x] * count < sum) {
						++board[y][x];
					}
				}
			}
		}
	}
}

int main()
{
	scanf("%d %d %d", &N, &M, &T);
	for (int y = 0; y < N; ++y) {
		for (int x = 0; x < M; ++x) {
			scanf("%d", &board[y][x]);
		}
	}
	for (int i = 0; i < T; ++i) {
		int x, d, k;
		scanf("%d %d %d", &x, &d, &k);
		solve(x, d, k);
	}
	int ret = 0;
	for (int y = 0; y < N; ++y) {
		for (int x = 0; x < M; ++x) {
			if (board[y][x] != DEL) {
				ret += board[y][x];
			}
		}
	}
	printf("%d\n", ret);
	return 0;
}

설정

트랙백

댓글

[삼성 SW 역량 테스트] 주사위 윷놀이

 

 

 

 

 

 

#include <stdio.h>

const int board[33][6] = {
	{0,1,2,3,4,5},
	{2,2,3,4,5,6},
	{4,3,4,5,6,7},
	{6,4,5,6,7,8},
	{8,5,6,7,8,9},
	{10,21,22,23,24,25},
	{12,7,8,9,10,11},
	{14,8,9,10,11,12},
	{16,9,10,11,12,13},
	{18,10,11,12,13,14},
	{20,27,28,24,25,26},
	{22,12,13,14,15,16},
	{24,13,14,15,16,17},
	{26,14,15,16,17,18},
	{28,15,16,17,18,19},
	{30,29,30,31,24,25},
	{32,17,18,19,20,32},
	{34,18,19,20,32,32},
	{36,19,20,32,32,32},
	{38,20,32,32,32,32},
	{40,32,32,32,32,32},
	{13,22,23,24,25,26},
	{16,23,24,25,26,20},
	{19,24,25,26,20,32},
	{25,25,26,20,32,32},
	{30,26,20,32,32,32},
	{35,20,32,32,32,32},
	{22,28,24,25,26,20},
	{24,24,25,26,20,32},
	{28,30,31,24,25,26},
	{27,31,24,25,26,20},
	{26,24,25,26,20,32},
	{0,32,32,32,32,32}
};

int in[10];

int get_score(int state) {
	int ret = 0;
	bool visited[33] = { false, };
	int pos[4] = { 0, };
	for (int turn = 0; turn < 10; ++turn) {
		int move = in[turn];
		int horse = (state >> (turn * 2)) & 0x03;
		int& cur_pos = pos[horse];
		int next_pos = board[cur_pos][move];
		int add_score = board[next_pos][0];

		if (visited[next_pos] && next_pos != 32) {
			return -1;
		}

		ret += add_score;
		visited[cur_pos] = false;
		visited[next_pos] = true;
		cur_pos = next_pos;
	}

	return ret;
}

int main()
{
	for (int i = 0; i < 10; ++i) {
		scanf("%d", &in[i]);
	}

	int result = 0;
	for (int state = 0; state < (1 << 20); ++state) {
		int candi = get_score(state);
		if (result < candi) {
			result = candi;
		}
	}

	printf("%d\n", result);
	return 0;
}

설정

트랙백

댓글