출처 - 백준사이트
나의 풀이
#include <iostream>
#include <cstring>
using namespace std;
const int dy[] = {0, -1, -1, 0, 1, 1, 1, 0, -1};
const int dx[] = {0, 0, -1, -1, -1, 0, 1, 1, 1};
typedef enum State {EMPTY, SHARK, FISH};
typedef struct info {
State state;
int num;
int dir;
} Info;
Info map[4][4];
int result = 0;
void moving_fish(Info (*pmap)[4]) {
for (int i=1 ; i<=16 ; i++) {
bool isBreak = false;
for (int y=0 ; y<4 && !isBreak ; y++) {
for (int x=0 ; x<4 && !isBreak; x++) {
if (pmap[y][x].state == FISH && pmap[y][x].num == i) {
int ny=0, nx=0, nd=pmap[y][x].dir;
while (!isBreak) {
ny = y + dy[nd];
nx = x + dx[nd];
if (-1<ny && ny<4 && -1<nx && nx<4 && (pmap[ny][nx].state != SHARK)) {
State t_state = pmap[ny][nx].state;
int t_num = pmap[ny][nx].num;
int t_dir = pmap[ny][nx].dir;
pmap[ny][nx].state = pmap[y][x].state;
pmap[ny][nx].num = pmap[y][x].num;
pmap[ny][nx].dir = nd;
pmap[y][x].state = t_state;
pmap[y][x].num = t_num;
pmap[y][x].dir = t_dir;
isBreak = true;
break;
}
if (!isBreak) {
nd++;
if (nd == 9)
nd = 1;
}
if (nd == pmap[y][x].dir) {
isBreak = true;
break;
}
}
}
}
}
}
}
void moving(int sy, int sx, int t_result, Info (*pmap)[4]) {
Info t_map[4][4];
int eat = t_result;
memset(t_map, 0, sizeof(t_map));
memcpy(t_map, pmap, sizeof(t_map));
t_map[sy][sx].state = SHARK;
eat += t_map[sy][sx].num;
moving_fish(t_map);
for (int i=1 ; i<=3 ; i++) {
int ny = sy + (dy[t_map[sy][sx].dir] * i);
int nx = sx + (dx[t_map[sy][sx].dir] * i);
if (-1<ny && ny<4 && -1<nx && nx<4 && (t_map[ny][nx].state == FISH)) {
int t_num = t_map[sy][sx].num;
int t_dir = t_map[sy][sx].dir;
t_map[sy][sx].state = EMPTY;
t_map[sy][sx].num = 0;
t_map[sy][sx].dir = 0;
moving(ny, nx, eat, t_map);
t_map[sy][sx].state = SHARK;
t_map[sy][sx].num = t_num;
t_map[sy][sx].dir = t_dir;
}
}
if (eat > result)
result = eat;
}
int main(void) {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
for (int y=0 ; y<4; y++) {
for (int x=0 ; x<4 ; x++) {
cin >> map[y][x].num >> map[y][x].dir;
map[y][x].state = FISH;
}
}
moving(0, 0, result, map);
cout << result;
return 0;
}
728x90
'Algorithm > 삼성 SW 역량 테스트 기출 문제' 카테고리의 다른 글
[Baekjoon][20055] 컨베이어 벨트 위의 로봇 (1) | 2022.06.27 |
---|---|
[Baekjoon][19238] 스타트 택시 (0) | 2022.06.23 |
[Baekjoon][16236] 아기 상어 (1) | 2022.03.08 |
[Baekjoon][14502] 연구소 (1) | 2022.03.08 |
[Baekjoon][14501] 퇴사 (0) | 2022.03.08 |
댓글