charminseok
[백준] 16236. 아기 상어 본문
아기상어가 물고기를 먹으면서 움직인다.
1. 아기상어가 움직일 수 있는 칸으로 이동하고(BFS사용) 먹을 수 있는 물고기라면 tmp 벡터에 저장한다.
2. tmp 벡터를 이동시간, 위치에 따라 정렬한다.
3. tmp[0]에 있는 물고기를 먹고 아기상어의 위치를 옮긴다.
아기 상어의 정보를 따로 저장하였고, 먹은 물고기의 수가 크기와 같아지면 size++를 해주었다. bfs()의 반환값이 0(먹을 물고기가 없을때)이되면 while문을 종료하고 정답을 출력했다.
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
|
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
int n;
vector<vector<int>> map;
vector<vector<bool>> visit;
vector<vector<pair<int,int>>> fish;
int dxy[4][2] = { {-1,0}, {0,-1},{1,0},{0,1} };
struct bs {
int x, y, size = 2, eat_cnt = 0;
}baby;
struct info {
int x, y, cnt;
};
bool cmp(const pair<pair<int,int>,int> &a, pair<pair<int, int>, int> &b) {
if (a.second == b.second) {
if (a.first.first == b.first.first) {
return a.first.second < b.first.second;
}
return a.first.first < b.first.first;
}
else
return a.second < b.second;
}
int sec;
int bfs() {
queue<info> q;
vector<pair<pair<int, int>, int>> tmp;
q.push({ baby.x,baby.y,0 });
visit[baby.x][baby.y] = true;
while (!q.empty()) {
int x = q.front().x;
int y = q.front().y;
int cnt = q.front().cnt;
q.pop();
for (int i = 0; i < 4; i++) {
int nx = x + dxy[i][0];
int ny = y + dxy[i][1];
if (nx < 0 || nx >= n || ny < 0 || ny >= n) continue;
if (visit[nx][ny]) continue;
if (map[nx][ny] <= baby.size) {
visit[nx][ny] = true;
q.push({ nx,ny,cnt + 1 });
if (map[nx][ny] != 0 && baby.size > map[nx][ny])
tmp.push_back({ { nx,ny },cnt + 1 });
}
}
}
sort(tmp.begin(), tmp.end(), cmp);
if (!tmp.empty()) {
int x = tmp[0].first.first;
int y = tmp[0].first.second;
baby.eat_cnt++;
baby.x = x;
baby.y = y;
map[x][y] = 0;
if (baby.eat_cnt == baby.size) {
baby.size++;
baby.eat_cnt = 0;
}
return tmp[0].second;
}
return 0;
}
int solution() {
int answer = 0;
cin >> n;
baby.size = 2;
map.assign(n, vector<int>(n));
fish.assign(7, vector<pair<int, int>>());
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> map[i][j];
if (map[i][j] == 9) {
baby.x = i, baby.y = j;
map[i][j] = 0;
}
if (0 < map[i][j] && map[i][j] <= 6)
fish[map[i][j]].push_back({ i,j });
}
}
while (true) {
visit.assign(n, vector<bool>(n, false));
int tmp = bfs();
if (tmp == 0) break;
answer += tmp;
}
return answer;
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
//freopen("Input.txt", "r", stdin);
cout<<solution();
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
'알고리즘 > 알고리즘 문제' 카테고리의 다른 글
[삼성 swea] 보물상자 비밀번호 (0) | 2019.09.22 |
---|---|
[백준] 12100. 2048 (easy) (0) | 2019.09.18 |
[삼성 swea] 5650. 핀볼 게임 (0) | 2019.09.13 |
[카카오 코딩테스트] 추석트래픽 (0) | 2019.09.11 |
[삼성 swea] 1949. 등산로 조성 (0) | 2019.09.11 |