검색결과 리스트
역량평가에 해당되는 글 27건
- 2020.10.05 [삼성 SW 역량 테스트] 큐빙 4
- 2020.09.23 [삼성 SW 역량 테스트] 스타트 택시 1
- 2020.09.16 [삼성 SW 역량 테스트] 어른 상어
- 2020.09.09 [삼성 SW 역량 테스트] 청소년 상어 2
- 2019.02.13 Linked List 5
글
[삼성 SW 역량 테스트] 큐빙
삼성 SW 역량 테스트 기출 풀이
2020. 10. 5. 21:56
그렇게 좋아 보이는 문제는 아니라 동영상 강의는 만들지 않았는데
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 역량 테스트] 스타트 택시
삼성 SW 역량 테스트 기출 풀이
2020. 9. 23. 21:59
#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 역량 테스트] 어른 상어
삼성 SW 역량 테스트 기출 풀이
2020. 9. 16. 21:46
#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 역량 테스트] 청소년 상어
삼성 SW 역량 테스트 기출 풀이
2020. 9. 9. 19:35
#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;
}
글
Linked List
Data Structure
2019. 2. 13. 23:00
#include <stdio.h> #include <algorithm> #include <list> #include "Windows.h" using namespace std; struct NODE { int prev; int next; int val; }; const int NODE_SIZE = 30000; //TEST CMD const int PUSH_BACK = 0; const int PUSH_FRONT = 1; const int INSERT = 2; const int POP_BACK = 3; const int POP_FRONT = 4; const int ERASE = 5; int test_cmd[NODE_SIZE][3]; struct MY_LIST { int HEAD = NODE_SIZE; int TAIL = NODE_SIZE + 1; int pos; NODE node[NODE_SIZE + 2]; MY_LIST() { pos = 0; node[HEAD].next = TAIL; node[TAIL].prev = HEAD; } void push_back(int data) { int prev = node[TAIL].prev; int next = node[prev].next; // TAIL; node[pos].val = data; node[pos].prev = prev; node[prev].next = pos; node[pos].next = next; node[next].prev = pos; ++pos; } void push_front(int data) { int next = node[HEAD].next; int prev = node[next].prev; // HEAD node[pos].val = data; node[pos].prev = prev; node[prev].next = pos; node[pos].next = next; node[next].prev = pos; ++pos; } void insert(int p, int data) { int next = node[HEAD].next; for(int i = 0; i < p; ++i) { next = node[next].next; } int prev = node[next].prev; node[pos].val = data; node[pos].prev = prev; node[prev].next = pos; node[pos].next = next; node[next].prev = pos; ++pos; } void pop_back() { int target = node[TAIL].prev; int prev = node[target].prev; int next = node[target].next; node[prev].next = next; node[next].prev = prev; } void pop_front() { int target = node[HEAD].next; int prev = node[target].prev; int next = node[target].next; node[prev].next = next; node[next].prev = prev; } void erase(int p) { int target = node[HEAD].next; for (int i = 0; i < p; ++i) { target = node[target].next; } int prev = node[target].prev; int next = node[target].next; node[prev].next = next; node[next].prev = prev; } }; MY_LIST my_list; list<int> stl_list; int main() { // make test case.. int cur_size = 0; for (int i = 0; i < NODE_SIZE; ++i) { if (i < NODE_SIZE / 3) { test_cmd[i][0] = rand() % 2; } else { test_cmd[i][0] = rand() % 6; } switch (test_cmd[i][0]) { case PUSH_BACK: case PUSH_FRONT: { test_cmd[i][1] = rand(); ++cur_size; break; } case INSERT: { test_cmd[i][1] = rand() % cur_size; test_cmd[i][2] = rand(); ++cur_size; break; } case POP_BACK: case POP_FRONT: { --cur_size; break; } case ERASE: { test_cmd[i][1] = rand() % cur_size; --cur_size; break; } } } // test my list int my_list_begin = GetTickCount(); for (int i = 0; i < NODE_SIZE; ++i) { switch (test_cmd[i][0]) { case PUSH_BACK: { my_list.push_back(test_cmd[i][1]); break; } case PUSH_FRONT: { my_list.push_front(test_cmd[i][1]); break; } case INSERT: { my_list.insert(test_cmd[i][1], test_cmd[i][2]); break; } case POP_BACK: { my_list.pop_back(); break; } case POP_FRONT: { my_list.pop_front(); break; } case ERASE: { my_list.erase(test_cmd[i][1]); break; } } } int my_list_end = GetTickCount(); // test stl list int stl_list_begin = GetTickCount(); for (int i = 0; i < NODE_SIZE; ++i) { switch (test_cmd[i][0]) { case PUSH_BACK: { stl_list.push_back(test_cmd[i][1]); break; } case PUSH_FRONT: { stl_list.push_front(test_cmd[i][1]); break; } case INSERT: { list<int>::iterator it = stl_list.begin(); for (int k = 0; k < test_cmd[i][1]; ++k) { ++it; } stl_list.insert(it, test_cmd[i][2]); break; } case POP_BACK: { stl_list.pop_back(); break; } case POP_FRONT: { stl_list.pop_front(); break; } case ERASE: { list<int>::iterator it = stl_list.begin(); for (int k = 0; k < test_cmd[i][1]; ++k) { ++it; } stl_list.erase(it); break; } } } int stl_list_end = GetTickCount(); //time compare printf("my list : %d\n", (my_list_end - my_list_begin)); printf("stl list : %d\n", (stl_list_end - stl_list_begin)); //result test list<int>::iterator it = stl_list.begin(); int cur = my_list.node[my_list.HEAD].next; while (it != stl_list.end()) { if (*it != my_list.node[cur].val) { printf("Error\n"); } ++it; cur = my_list.node[cur].next; } return 0; }