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

설정

트랙백

댓글