[삼성 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 역량 테스트] 큐빙

그렇게 좋아 보이는 문제는 아니라 동영상 강의는 만들지 않았는데

AC 받은 코드만 공개 합니다.

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

const int WW = 0;
const int YY = 1;
const int RR = 2;
const int OO = 3;
const int GG = 4;
const int BB = 5;

char COLOR[] = "wyrogb";

char cube[6][9];
char backup[6][9];

void init() {
	for (int f = 0; f < 6; ++f) {
		for (int i = 0; i < 9; ++i) {
			cube[f][i] = COLOR[f];
		}
	}
}

void cw(int face) {
	cube[face][0] = backup[face][6];
	cube[face][1] = backup[face][3];
	cube[face][2] = backup[face][0];

	cube[face][3] = backup[face][7];
	cube[face][4] = backup[face][4];
	cube[face][5] = backup[face][1];

	cube[face][6] = backup[face][8];
	cube[face][7] = backup[face][5];
	cube[face][8] = backup[face][2];
}

void rotate(char face) {
	for (int f = 0; f < 6; f++) {
		for (int i = 0; i < 9; ++i) {
			backup[f][i] = cube[f][i];
		}
	}

	if (face == 'F') {
		cube[WW][6] = backup[GG][6];
		cube[WW][7] = backup[GG][7];
		cube[WW][8] = backup[GG][8];

		cube[BB][6] = backup[WW][6];
		cube[BB][7] = backup[WW][7];
		cube[BB][8] = backup[WW][8];

		cube[YY][2] = backup[BB][6];
		cube[YY][1] = backup[BB][7];
		cube[YY][0] = backup[BB][8];

		cube[GG][6] = backup[YY][2];
		cube[GG][7] = backup[YY][1];
		cube[GG][8] = backup[YY][0];

		cw(RR);
	}

	
	if (face == 'B') {
		cube[YY][6] = backup[GG][2];
		cube[YY][7] = backup[GG][1];
		cube[YY][8] = backup[GG][0];

		cube[BB][2] = backup[YY][6];
		cube[BB][1] = backup[YY][7];
		cube[BB][0] = backup[YY][8];

		cube[WW][2] = backup[BB][2];
		cube[WW][1] = backup[BB][1];
		cube[WW][0] = backup[BB][0];

		cube[GG][2] = backup[WW][2];
		cube[GG][1] = backup[WW][1];
		cube[GG][0] = backup[WW][0];

		cw(OO);
	}

	if (face == 'U') {
		cube[OO][6] = backup[GG][8];
		cube[OO][7] = backup[GG][5];
		cube[OO][8] = backup[GG][2];

		cube[BB][0] = backup[OO][6];
		cube[BB][3] = backup[OO][7];
		cube[BB][6] = backup[OO][8];

		cube[RR][2] = backup[BB][0];
		cube[RR][1] = backup[BB][3];
		cube[RR][0] = backup[BB][6];

		cube[GG][8] = backup[RR][2];
		cube[GG][5] = backup[RR][1];
		cube[GG][2] = backup[RR][0];

		cw(WW);
	}

	if (face == 'D') {
		cube[RR][6] = backup[GG][0];
		cube[RR][7] = backup[GG][3];
		cube[RR][8] = backup[GG][6];

		cube[BB][8] = backup[RR][6];
		cube[BB][5] = backup[RR][7];
		cube[BB][2] = backup[RR][8];

		cube[OO][2] = backup[BB][8];
		cube[OO][1] = backup[BB][5];
		cube[OO][0] = backup[BB][2];

		cube[GG][0] = backup[OO][2];
		cube[GG][3] = backup[OO][1];
		cube[GG][6] = backup[OO][0];

		cw(YY);
	}

	if (face == 'R') {
		cube[OO][8] = backup[WW][8];
		cube[OO][5] = backup[WW][5];
		cube[OO][2] = backup[WW][2];

		cube[YY][8] = backup[OO][8];
		cube[YY][5] = backup[OO][5];
		cube[YY][2] = backup[OO][2];

		cube[RR][8] = backup[YY][8];
		cube[RR][5] = backup[YY][5];
		cube[RR][2] = backup[YY][2];

		cube[WW][8] = backup[RR][8];
		cube[WW][5] = backup[RR][5];
		cube[WW][2] = backup[RR][2];

		cw(BB);
	}

	if (face == 'L') {
		cube[OO][0] = backup[YY][0];
		cube[OO][3] = backup[YY][3];
		cube[OO][6] = backup[YY][6];

		cube[WW][0] = backup[OO][0];
		cube[WW][3] = backup[OO][3];
		cube[WW][6] = backup[OO][6];

		cube[RR][0] = backup[WW][0];
		cube[RR][3] = backup[WW][3];
		cube[RR][6] = backup[WW][6];

		cube[YY][0] = backup[RR][0];
		cube[YY][3] = backup[RR][3];
		cube[YY][6] = backup[RR][6];

		cw(GG);
	}
}
int main()
{
	int tc;
	scanf("%d", &tc);
	while (tc--) {
		int n;
		scanf("%d", &n);
		init();
		char buf[10];
		for (int i = 0; i < n; ++i) {
			scanf("%s", buf);
			int k = 1;
			if (buf[1] == '-') {
				k = 3;
			}
			for (int j = 0; j < k; ++j) {
				rotate(buf[0]);
			}
		}

		for (int i = 0; i < 9; ++i) {
			if (i != 0 && i % 3 == 0)	printf("\n");
			printf("%c", cube[WW][i]);
		}
		printf("\n");
	}
	return 0;	
}

설정

트랙백

댓글

[삼성 SW 역량 테스트] 스타트 택시

 

 

 

 

 

 

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

struct CUSTOMER {
	int start;
	int end;
};

struct TAXI {
	int pos;
	int distance;
};

const int WALL = -1;
const int EMPTY = -2;
const int dy[4] = { -1, 0, +1, 0 };
const int dx[4] = { 0, -1, 0, +1 };

int N, M, fuel;
int taxi_y, taxi_x;
int board[20][20];
CUSTOMER customer[400];

int find_customer() {
	queue<TAXI> q;
	bool visited[20][20] = { false, };
	TAXI cur = { taxi_y * 100 + taxi_x, 0 };
	visited[taxi_y][taxi_x] = true;
	q.push(cur);

	int candi_size = 0;
	int candi[400] = { 0, };
	int candi_distance = -1;

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

		if (candi_distance != -1 && cur.distance > candi_distance) {
			break;
		}

		int y = cur.pos / 100;
		int x = cur.pos % 100;
		if (board[y][x] >= 0) {
			candi[candi_size++] = board[y][x];
			candi_distance = cur.distance;
		}

		for (int d = 0; d < 4; ++d) {
			int ny = y + dy[d];
			int nx = x + dx[d];
			if (ny < 0 || ny >= N || nx < 0 || nx >= N
				|| board[ny][nx] == WALL || visited[ny][nx] == true) {
				continue;
			}
			visited[ny][nx] = true;
			TAXI next = { ny * 100 + nx, cur.distance + 1 };
			q.push(next);
		}
	}
	if (candi_distance > fuel) {
		return -1;
	}
	int ret = -1;
	int candi_val = 10000;
	for (int i = 0; i < candi_size; ++i) {
		if (candi_val > customer[candi[i]].start) {
			candi_val = customer[candi[i]].start;
			ret = candi[i];
		}
	}
	
	taxi_y = customer[ret].start / 100;
	taxi_x = customer[ret].start % 100;
	board[taxi_y][taxi_x] = EMPTY;
	fuel -= candi_distance;
	return ret;
}

bool move_customer(int target) {
	queue<TAXI> q;
	bool visited[20][20] = { false, };
	TAXI cur = { taxi_y * 100 + taxi_x, 0 };
	visited[taxi_y][taxi_x] = true;
	q.push(cur);

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

		if (cur.distance > fuel) {
			return false;
		}

		if (cur.pos == customer[target].end) {
			taxi_y = customer[target].end / 100;
			taxi_x = customer[target].end % 100;
			fuel += cur.distance;
			return true;
		}

		int y = cur.pos / 100;
		int x = cur.pos % 100;
		for (int d = 0; d < 4; ++d) {
			int ny = y + dy[d];
			int nx = x + dx[d];
			if (ny < 0 || ny >= N || nx < 0 || nx >= N
				|| board[ny][nx] == WALL || visited[ny][nx] == true) {
				continue;
			}
			TAXI next = { ny * 100 + nx, cur.distance + 1 };
			q.push(next);
			visited[ny][nx] = true;
		}
	}
	return false;
}

int solve() {
	int ret = -1;
	for (int i = 0; i < M; ++i) {
		int target = find_customer();
		if (target == -1) {
			return ret;
		}
		bool success = move_customer(target);
		if (success == false) {
			return ret;
		}
	}
	ret = fuel;
	return ret;
}
int main()
{
	scanf("%d %d %d", &N, &M, &fuel);
	for (int y = 0; y < N; ++y) {
		for (int x = 0; x < N; ++x) {
			scanf("%d", &board[y][x]);
			board[y][x] -= 2;
		}
	}
	scanf("%d %d", &taxi_y, &taxi_x);
	--taxi_y, --taxi_x;
	for (int i = 0; i < M; ++i) {
		int sy, sx, ey, ex;
		scanf("%d %d %d %d", &sy, &sx, &ey, &ex);
		--sy, --sx, --ey, --ex;
		customer[i].start = (sy * 100 + sx);
		customer[i].end = (ey * 100 + ex);
		board[sy][sx] = i;
	}
	int ret = solve();
	printf("%d\n", ret);
	return 0;
}

설정

트랙백

댓글

[삼성 SW 역량 테스트] 어른 상어

 

 

 

 

 

 

#include <stdio.h>

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

struct SHARK {
	int y, x, d;
	int priority[4][4];
};

int N, M, K, ret;
int board[20][20][3];
SHARK shark[400];

void solve() {
	int time = 0;
	int kill_shark = 0;
	while (time <= 1000) {
		++time;

		int new_board[20][20][3] = { 0, };
		for (int y = 0; y < N; ++y) {
			for (int x = 0; x < N; ++x) {
				new_board[y][x][0] = board[y][x][0];
				new_board[y][x][2] = board[y][x][2];
				if (new_board[y][x][2] > 0) {
					--new_board[y][x][2];
				}
				if (new_board[y][x][2] > 0) {
					new_board[y][x][1] = board[y][x][1];
				}
			}
		}

		for (int i = 0; i < M; ++i) {
			int cy = shark[i].y;
			int cx = shark[i].x;
			int cd = shark[i].d;
			if (cy == -1) {
				continue;
			}
			bool is_move = false;
			for(int d = 0; d < 4; ++d){
				int nd = shark[i].priority[cd][d];
				int ny = cy + dy[nd];
				int nx = cx + dx[nd];
				if (ny < 0 || ny >= N || nx < 0 || nx >= N || board[ny][nx][2] != 0) {
					continue;
				}
				is_move = true;
				new_board[cy][cx][0] = 0;
				if (new_board[ny][nx][0] == 0) {
					new_board[ny][nx][0] = i + 1;
					new_board[ny][nx][1] = i + 1;
					new_board[ny][nx][2] = K;
					
					shark[i].y = ny;
					shark[i].x = nx;
					shark[i].d = nd;
				}
				else {
					++kill_shark;
					shark[i].y = -1;
				}
				break;
			}
			if (is_move == false) {
				for (int d = 0; d < 4; ++d) {
					int nd = shark[i].priority[cd][d];
					int ny = cy + dy[nd];
					int nx = cx + dx[nd];
					if (ny < 0 || ny >= N || nx < 0 || nx >= N) {
						continue;
					}
					if (board[ny][nx][2] != 0 && board[ny][nx][1] != i + 1) {
						continue;
					}
					new_board[cy][cx][0] = 0;
					if (new_board[ny][nx][0] == 0) {
						new_board[ny][nx][0] = i + 1;
						new_board[ny][nx][1] = i + 1;
						new_board[ny][nx][2] = K;

						shark[i].y = ny;
						shark[i].x = nx;
						shark[i].d = nd;
					}
					else {
						++kill_shark;
						shark[i].y = -1;
					}
					break;
				}
			}
		}
		
		if (kill_shark == M - 1) {
			break;
		}
		for (int y = 0; y < N; ++y) {
			for (int x = 0; x < N; ++x) {
				board[y][x][0] = new_board[y][x][0];
				board[y][x][1] = new_board[y][x][1];
				board[y][x][2] = new_board[y][x][2];
			}
		}
	}
	if (time <= 1000) {
		ret = time;
	}
}

int main()
{
	scanf("%d %d %d", &N, &M, &K);
	for (int y = 0; y < N; ++y) {
		for (int x = 0; x < N; ++x) {
			scanf("%d", &board[y][x][0]);
			if (board[y][x][0] != 0) {
				int shark_number = board[y][x][0] - 1;
				shark[shark_number].y = y;
				shark[shark_number].x = x;
				board[y][x][1] = board[y][x][0];
				board[y][x][2] = K;
			}
		}
	}
	for (int i = 0; i < M; ++i) {
		scanf("%d", &shark[i].d);
		--shark[i].d;
	}

	for (int i = 0; i < M; ++i) {
		for (int d = 0; d < 4; ++d) {
			scanf("%d %d %d %d", &shark[i].priority[d][0], &shark[i].priority[d][1], &shark[i].priority[d][2], &shark[i].priority[d][3]);
			--shark[i].priority[d][0], --shark[i].priority[d][1], --shark[i].priority[d][2], --shark[i].priority[d][3];
		}
	}

	ret = -1;
	solve();
	printf("%d\n", ret);
	return 0;
}

설정

트랙백

댓글