[삼성 SW 역량 테스트] 청소년 상어

 

 

 

 

 

#include <stdio.h>

struct FISH {
	int y, x;
	int dir;
};

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 ret;

void solve(int board[4][4], FISH fish[], int shark_y, int shark_x, int sum) {
	int candi_board[4][4];
	FISH candi_fish[16];
	for (int y = 0; y < 4; ++y) {
		for (int x = 0; x < 4; ++x) {
			candi_board[y][x] = board[y][x];
		}
	}
	for (int i = 0; i < 16; ++i) {
		candi_fish[i] = fish[i];
	}

	//eat
	int fish_number = candi_board[shark_y][shark_x];
	int shark_dir = candi_fish[fish_number].dir;
	candi_fish[fish_number].y = -1;
	candi_fish[fish_number].x = -1;
	candi_fish[fish_number].dir = -1;
	candi_board[shark_y][shark_x] = -1;

	sum += (fish_number + 1);
	if (ret < sum) {
		ret = sum;
	}

	//fish move
	for (int f = 0; f < 16; ++f) {
		if (candi_fish[f].y == -1) {
			continue;
		}
		int cy = candi_fish[f].y;
		int cx = candi_fish[f].x;
		int cd = candi_fish[f].dir;

		int ny = cy + dy[cd];
		int nx = cx + dx[cd];
		int nd = cd;
		while (ny < 0 || ny >= 4 || nx < 0 || nx >= 4 || (ny == shark_y && nx == shark_x)) {
			nd = (nd + 1) % 8;
			ny = cy + dy[nd];
			nx = cx + dx[nd];
		}

		if (candi_board[ny][nx] != -1) {
			int target = candi_board[ny][nx];
			candi_fish[target].y = cy;
			candi_fish[target].x = cx;
			candi_fish[f].y = ny;
			candi_fish[f].x = nx;
			candi_fish[f].dir = nd;

			candi_board[ny][nx] = f;
			candi_board[cy][cx] = target;
		}
		else {
			candi_fish[f].y = ny;
			candi_fish[f].x = nx;
			candi_fish[f].dir = nd;

			candi_board[ny][nx] = f;
			candi_board[cy][cx] = -1;
		}
	}

	//shark move
	for (int step = 1; step < 4; ++step) {
		int ny = shark_y + dy[shark_dir] * step;
		int nx = shark_x + dx[shark_dir] * step;
		if (ny < 0 || ny >= 4 || nx < 0 || nx >= 4) {
			break;
		}
		if (candi_board[ny][nx] != -1) {
			solve(candi_board, candi_fish, ny, nx, sum);
		}
	}
}

int main()
{
	FISH fish[16];
	int board[4][4];
	for (int y = 0; y < 4; ++y) {
		for (int x = 0; x < 4; ++x) {
			int a, b;
			scanf("%d %d", &a, &b);
			--a, --b;
			fish[a].y = y;
			fish[a].x = x;
			fish[a].dir = b;
			board[y][x] = a;
		}
	}
	ret = 0;
	solve(board, fish, 0, 0, 0);
	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;
}

설정

트랙백

댓글

[LeetCode][0004] median of Two Sorted Arrays

LeetCode 2020. 6. 12. 00:27

 

 

 

class Solution {
public:
    double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
        int nums1_size = nums1.size();
        int nums2_size = nums2.size();
        
        if(nums1_size > nums2_size) {
            return findMedianSortedArrays(nums2, nums1);
        }
        
        int left = 0, right = nums1_size;
        int mid = (nums1_size + nums2_size + 1) / 2;
        
        int max_of_left = -1, min_of_right = -1;
        while(left <= right) {
            int i = (left + right) / 2;
            int j = (mid - i);
            
            if(i < nums1_size && nums1[i] < nums2[j - 1]) {
                left = i + 1;
            } else if(i > 0 && nums1[i - 1] > nums2[j]) {
                right = i - 1;
            } else {
                if(i == 0) {
                    max_of_left = nums2[j - 1];
                } else if(j == 0) {
                    max_of_left = nums1[i - 1];
                } else {
                    max_of_left = max(nums1[i - 1], nums2[j - 1]);
                }
                
                if((nums1_size + nums2_size) % 2 == 1) {
                    return max_of_left;
                }
                
                if(i == nums1_size) {
                    min_of_right = nums2[j];
                } else if(j == nums2_size) {
                    min_of_right = nums1[i];
                } else {
                    min_of_right = min(nums1[i], nums2[j]);
                }
                return (max_of_left + min_of_right) / 2.0;
            }
        }
        return -1.0;
    }
};

설정

트랙백

댓글