문제 링크입니다 https://www.acmicpc.net/problem/16236
1. 아기상어(나)가 먹을 수 있는(크기가 나보다 작은) 물고기들을 찾는다.
2. 나로부터 찾은 물고기들까지의 거리를 bfs를 통해 거리를 나타내는 배열에 저장한다.
3. 먹을 수 있는 물고기들 중, 거리가 가장 가까우면서 가장 위쪽, 가장 왼쪽에 있는 물고기를 먹는다.
(물고기를 내 크기만큼 먹으면 내 크기를 증가시켜야 함)
4. 그 물고기의 좌표에 해당하는 거리 배열의 좌표(=거리), 결국 그 물고기를 먹는 데까지 걸린 시간이다.
5. 그 시간을 최종 결과값에 누적하여 더해준다.
-> 1번 ~ 5번을 반복하다가 더 이상 먹을 수 있는 물고기를 찾지 못하면 누적 결과값을 출력하고 종료한다.
[주석 + 소스코드]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
// [백준] 삼성 SW 역량 테스트 기출 문제 : 아기 상어(16236)
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
const int MAX = 20 + 10;
// 좌표를 저장하기 위한 구조체
struct p {
int y, x;
};
// 아기상어의 시작좌표(sy, sx), 상어가 먹어야하는 좌표(min_y, min_x)
// 거리 배열 dist[MAX][MAX], 상어의 현재 크기 shark_size, 최종 결과값 ret
int n, map[MAX][MAX], sy, sx, min_y, min_x, shark_size, ret, dist[MAX][MAX];
const int dy[] = { -1, 0, 1, 0 };
const int dx[] = { 0, -1, 0, 1 };
// 좌표를 인자로 받아서 p type으로 바꿔주는 함수
p make_p(int y, int x) {
p temp;
temp.y = y, temp.x = x;
return temp;
}
// 입력을 받는 함수
void input(void) {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &map[i][j]);
if (map[i][j] == 9) {
sy = i, sx = j; // 아기상어의 초기 위치 저장
shark_size = 2; // 아기상어의 처음 크기 2
}
}
}
}
// 아기상어의 위치로부터 현재 크기보다 작은 물고기들까지의 거리를 dist 배열에 bfs를 통해 최신화하는 함수
void update_dist(int y, int x) {
bool check[MAX][MAX] = { 0, }; // bfs를 위한 visit 체크 배열 선언
int result[MAX][MAX] = { 0, }; // bfs로 뻗어 나가면서 거리를 저장할 배열 선언
queue <p> q;
check[y][x] = true;
q.push(make_p(y, x));
while (!q.empty()) {
p cur = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int yy = cur.y + dy[i];
int xx = cur.x + dx[i];
// 다음 칸의 물고기의 크기가 내 크기보다 크거나, 이미 방문 했던 칸이거나, 맵을 벗어나면
if (map[yy][xx] > shark_size || check[yy][xx] || yy < 0 || yy >= n || xx < 0 || xx >= n) {
continue;
}
result[yy][xx] = result[cur.y][cur.x] + 1; // result에 거리를 저장
q.push(make_p(yy, xx));
check[yy][xx] = true;
// 내 크기보다 작은 물고기를 발견하면 dist에 거리를 update
if (map[yy][xx] < shark_size) dist[yy][xx] = result[yy][xx];
}
}
}
// 나보다 작은 크기를 가진 물고기 중 가장 가깝고, 그 중에서도 가장 위쪽, 가장 왼쪽에 있는
// 물고기를 찾아서 그 좌표를 min_y, min_x 에 저장해주는 함수
int find_nearestFish(void) {
int min_ = 987987987;
// for문을 왼쪽 위에서부터 돌아주면 내가 먹을 수 있는 물고기 중
// 가장 위쪽, 왼쪽에 있는 물고기를 찾을 수 있음
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
// 거리가 0이 아니고, 거리가 가장 가깝고, 그 칸에 물고기가 내 크기보다 작다면
if (dist[i][j] != 0 && dist[i][j] < min_ && map[i][j] && map[i][j] < shark_size) {
min_ = dist[i][j];
min_y = i, min_x = j; // 먹을 수 있는 물고기의 좌표 저장
}
}
}
if (min_ == 987987987) return -1; // 발견하지 못했으면 -1을 반환
else {
map[min_y][min_x] = 0; // 그 물고기가 있는 칸을 빈 칸으로 만들고
return dist[min_y][min_x]; // 그 물고기까지의 거리를 반환(거리가 결국 시간)
}
}
int solve(void) {
input();
int y = sy, x = sx; // 맨 처음 아기상어의 위치
int catch_cnt = 0; // 먹은 물고기 수
while (1) {
memset(dist, 0, sizeof(dist)); // dist 배열 초기화
update_dist(y, x); // dist 배열 update
int time = find_nearestFish(); // 내가 먹을 수 있는 물고기를 먹었습니다
if (time == -1) return ret; // 아무것도 못 먹었네요
ret += time; // 구하고자 하는 것은 몇초동안 물고기를 먹을 수 있느냐
map[min_y][min_x] = 9; // 아기상어의 위치가 먹은 물고기의 좌표로 바뀜
map[y][x] = 0; // 원래 아기상어의 위치는 빈 칸으로
catch_cnt++; // 한마리 먹을 때마다 증가시키고
// 내가 먹은 물고기가 내 크기와 같다면
if (catch_cnt == shark_size) {
catch_cnt = 0, shark_size++; // 먹은 물고기 수 초기화 + 레벨업!
}
y = min_y, x = min_x; // 바뀐 아기상어의 위치 update
}
}
int main(void) {
printf("%d", solve());
return 0;
}
|
cs |
[주석 없는 소스코드]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
const int MAX = 20 + 10;
struct p {
int y, x;
};
int n, map[MAX][MAX], sy, sx, min_y, min_x, shark_size, ret, dist[MAX][MAX];
const int dy[] = { -1, 0, 1, 0 };
const int dx[] = { 0, -1, 0, 1 };
p make_p(int y, int x) {
p temp;
temp.y = y, temp.x = x;
return temp;
}
void input(void) {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &map[i][j]);
if (map[i][j] == 9) {
sy = i, sx = j;
shark_size = 2;
}
}
}
}
void update_dist(int y, int x) {
bool check[MAX][MAX] = { 0, };
int result[MAX][MAX] = { 0, };
queue <p> q;
check[y][x] = true;
q.push(make_p(y, x));
while (!q.empty()) {
p cur = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int yy = cur.y + dy[i];
int xx = cur.x + dx[i];
if (map[yy][xx] > shark_size || check[yy][xx] || yy < 0 || yy >= n || xx < 0 || xx >= n) {
continue;
}
result[yy][xx] = result[cur.y][cur.x] + 1;
q.push(make_p(yy, xx));
check[yy][xx] = true;
if (map[yy][xx] < shark_size) dist[yy][xx] = result[yy][xx];
}
}
}
int find_nearestFish(void) {
int min_ = 987987987;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (dist[i][j] != 0 && dist[i][j] < min_ && map[i][j] && map[i][j] < shark_size) {
min_ = dist[i][j];
min_y = i, min_x = j;
}
}
}
if (min_ == 987987987) return -1;
else {
map[min_y][min_x] = 0;
return dist[min_y][min_x];
}
}
int solve(void) {
input();
int y = sy, x = sx;
int catch_cnt = 0;
while (1) {
memset(dist, 0, sizeof(dist));
update_dist(y, x);
int time = find_nearestFish();
if (time == -1) return ret;
ret += time;
map[min_y][min_x] = 9;
map[y][x] = 0;
catch_cnt++;
if (catch_cnt == shark_size) catch_cnt = 0, shark_size++;
y = min_y, x = min_x;
}
}
int main(void) {
printf("%d", solve());
return 0;
}
|
cs |
'알고리즘 > BaekJoon' 카테고리의 다른 글
백준 1065 : 한수 (0) | 2020.02.17 |
---|---|
백준 2231 : 분해합 (0) | 2020.02.17 |
백준 14503 : 로봇 청소기 (0) | 2020.02.16 |
백준 14502 : 연구소 (0) | 2020.02.16 |
백준 2798 : 블랙잭 (0) | 2020.02.16 |