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

설정

트랙백

댓글

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

설정

트랙백

댓글