[BOJ] 6236 아기 상어

Time Lapse :NONE

16236.cpp

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
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <math.h>
#include <vector>
#include <queue>

using namespace std;

class Shark{
public:
int x;
int y;
int size;
int eat;
};

int map[20][20];
int dist[20][20];
bool visit[20][20];
int dx[4] = {0,1,0,-1};
int dy[4] = {-1,0,1,0};
int N;
int ans = 0;
pair<int,int> fish;
bool flag = true;
Shark shark;

void set_dist()
{
queue<pair<int,int>> q;
memset(visit,false,sizeof(visit));
dist[shark.y][shark.x] = 0;
visit[shark.y][shark.x] = true;
q.push(make_pair(shark.y,shark.x));

while(!q.empty())
{
int cur_x = q.front().second;
int cur_y = q.front().first;
q.pop();
for(int i=0;i<4;i++)
{
int next_x = cur_x + dx[i];
int next_y = cur_y + dy[i];
if(0<=next_x && next_x<N && 0<=next_y && next_y<N)
{
if(map[next_y][next_x]<=shark.size && !visit[next_y][next_x])
{
q.push(make_pair(next_y,next_x));
dist[next_y][next_x] = dist[cur_y][cur_x] + 1;
visit[next_y][next_x] = true;
}
}
}
}
}

void select_fish()
{
int min_dist = 987654321;
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
if(min_dist>dist[i][j]&&0<map[i][j]&&map[i][j]<shark.size){
min_dist = dist[i][j];
fish = make_pair(i,j);
flag = true;
}


return ;
}

void eat_fish()
{
shark.x = fish.second;
shark.y = fish.first;
shark.eat += 1;
if(shark.eat==shark.size)
{
shark.size += 1;
shark.eat = 0;
}
ans += dist[shark.y][shark.x];
map[shark.y][shark.x] = 0;
}

void find_fish()
{
flag = false;
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
dist[i][j] = 987654321;}}
set_dist();
select_fish();
if(flag)
eat_fish();
}

int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>N;
for(int i=0;i<N;i++)
for(int j=0;j<N;j++) {
cin >> map[i][j];
if(map[i][j]==9)
{
shark.x = j;
shark.y = i;
shark.size = 2;
shark.eat = 0;
map[i][j] = 0;
}
}

while(flag)
{
find_fish();
}
cout<<ans<<endl;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/16236/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.