반응형
#include<iostream>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
int arr[20][20];
bool visit[20][20];
int N;
struct Node {
int y;
int x;
int dis;
};
bool compare(Node a1, Node a2)
{
if (a1.dis != a2.dis)
{
return a1.dis < a2.dis;
}
else
{
if (a1.y != a2.y)
{
return a1.y < a2.y;
}
else
{
return a1.x < a2.x;
}
}
}
int dx[4] = { 0,0,-1,1 };
int dy[4] = { -1,1,0,0 };
int age, ate;
int main(void)
{
cin >> N;
queue<Node> shark;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
cin >> arr[i][j];
if (arr[i][j] == 9)
{
shark.push({ i,j,0 });
arr[i][j] = 0;
}
}
}
age = 2;
ate = 0;
int count = 0;
while (1)
{
vector<Node> fish;
memset(visit, 0, sizeof(visit));
while (!shark.empty())
{
int y = shark.front().y;
int x = shark.front().x;
int dis = shark.front().dis;
shark.pop();
for (int i = 0; i < 4; i++)
{
int newy = y + dy[i];
int newx = x + dx[i];
if (newy < N && newy >= 0 && newx < N && newx >= 0)
{
if (arr[newy][newx] <= age && !visit[newy][newx])
{
visit[newy][newx] = true;
if (arr[newy][newx] < age && arr[newy][newx])
{
fish.push_back({ newy,newx,dis + 1 });
}
shark.push({ newy,newx,dis + 1 });
}
}
}
}
if (fish.empty())
{
break;
}
sort(fish.begin(), fish.end(),compare);
shark.push({ fish[0].y, fish[0].x, 0 });
arr[fish[0].y][fish[0].x] = 0;
ate++;
count += fish[0].dis;
if (ate == age)
{
ate = 0;
age++;
}
}
cout << count << endl;
}
반응형
'Algorithm' 카테고리의 다른 글
백준 5525번 IOIOI (0) | 2020.07.07 |
---|---|
백분 16916번 부분문자열 (0) | 2020.07.07 |
백준 1213번 팰린드롬 만들기 (0) | 2020.07.06 |
7793. 오! 나의 여신님 (0) | 2020.07.06 |
백준 1120번 문자열 (0) | 2020.07.05 |