Algorithm๐Ÿฐ/๋ฐฑ์ค€

[๋ฐฑ์ค€] 16236 ์•„๊ธฐ ์ƒ์–ด DFS, Priority-Queue

Jouureee 2021. 5. 6. 16:51

๋ฌธ์ œ :

www.acmicpc.net/problem/16236

 

16236๋ฒˆ: ์•„๊ธฐ ์ƒ์–ด

N×N ํฌ๊ธฐ์˜ ๊ณต๊ฐ„์— ๋ฌผ๊ณ ๊ธฐ M๋งˆ๋ฆฌ์™€ ์•„๊ธฐ ์ƒ์–ด 1๋งˆ๋ฆฌ๊ฐ€ ์žˆ๋‹ค. ๊ณต๊ฐ„์€ 1×1 ํฌ๊ธฐ์˜ ์ •์‚ฌ๊ฐํ˜• ์นธ์œผ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ๋‹ค. ํ•œ ์นธ์—๋Š” ๋ฌผ๊ณ ๊ธฐ๊ฐ€ ์ตœ๋Œ€ 1๋งˆ๋ฆฌ ์กด์žฌํ•œ๋‹ค. ์•„๊ธฐ ์ƒ์–ด์™€ ๋ฌผ๊ณ ๊ธฐ๋Š” ๋ชจ๋‘ ํฌ๊ธฐ๋ฅผ ๊ฐ€

www.acmicpc.net

 

๋ฌธ์ œ ์„ค๋ช… :

 

-> ์•„๊ธฐ ์ƒ์–ด๊ฐ€ ๋ช‡์ดˆ ๋™์•ˆ ๋ฌผ๊ณ ๊ธฐ ์žก์•„๋จน์„ ์ˆ˜ ์žˆ๋Š”์ง€ ์ถœ๋ ฅํ•˜์‹œ์˜ค

 

๊ณต๊ฐ„ n = 4

  • 0: ๋นˆ ์นธ
  • 1, 2, 3, 4, 5, 6: ์นธ์— ์žˆ๋Š” ๋ฌผ๊ณ ๊ธฐ์˜ ํฌ๊ธฐ
  • 9: ์•„๊ธฐ ์ƒ์–ด์˜ ์œ„์น˜

์•„๊ธฐ ์ƒ์–ด๊ฐ€ ์›€์ง์ผ ์ˆ˜ ์žˆ๋Š” ์กฐ๊ฑด 

1. ์ž๊ธฐ ์ฃผ๋ณ€์— ์ธ์ ‘ํ•œ ๋ฌผ๊ณ ๊ธฐ && ๋จน์„ ์ˆ˜ ์žˆ๋‹ค๋ฉด ๊ฐ€์žฅ ์ธ์ ‘ํ•œ ๋ฌผ๊ณ ๊ธฐ๋ฅผ ๋จน์œผ๋Ÿฌ ๊ฐ„๋‹ค. 

2. ์ธ์ ‘ํ•œ ๋ฌผ๊ณ ๊ธฐ๊ฐ€ ์—ฌ๋Ÿฌ ๋งˆ๋ฆฌ๋ผ๋ฉด ๊ฐ€์žฅ ์œ„์— ์žˆ๋Š” ๋ฌผ๊ณ ๊ธฐ, ๊ฐ€์žฅ ์™ผ์ชฝ์— ์žˆ๋Š” ๋ฌผ๊ณ ๊ธฐ ์ˆœ์œผ๋กœ ๋จน๋Š”๋‹ค.

 

๋จน์„ ์ˆ˜ ์žˆ๋Š” ๋ฌผ๊ณ ๊ธฐ ์กฐ๊ฑด 

1. ์ž๊ธฐ ํฌ๊ธฐ๋ณด๋‹ค ์ž‘์€ ๋ฌผ๊ณ ๊ธฐ๋งŒ ๋จน์„ ์ˆ˜ ์žˆ๋‹ค.

2. ์ž๊ธฐ ํฌ๊ธฐ๋Š” ์ž๊ธฐ ํฌ๊ธฐ ๋งŒํผ์˜ ๋ฌผ๊ณ ๊ธฐ๋ฅผ ๋จน์œผ๋ฉด 1 ์ฆ๊ฐ€ํ•œ๋‹ค.

 

๋ถ„์„ : 

์ƒ์–ด์˜ ์›€์ง์ž„์„ bfs๋กœ ๋Œ๊ณ  q์—์„œ {์ด๋™ ๊ฑฐ๋ฆฌ, y, x}๋กœ ๊ด€๋ฆฌํ•œ๋‹ค.

n x n ๋ฐฐ์—ด์„ bfs๋กœ ํƒ์ƒ‰ํ•˜๋Š” ๋™์•ˆ ์ƒ์–ด๊ฐ€ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ณณ์— ๋Œ€ํ•ด์„œ๋งŒ q์— ๋„ฃ์œผ๋ฉฐ 

๋จน์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ 0์ด ์•„๋‹Œ ์นธ์— ๋Œ€ํ•ด pq (๋จน์„ ์ˆ˜ ์žˆ๋Š” ๋ฌผ๊ณ ๊ธฐ ๋ฐฐ์—ด) + (๊ทธ๋Ÿฐ๋ฐ ์ƒ์–ด๊ฐ€ ๋จน์„ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ๋œ) ์— ๋„ฃ๋Š”๋‹ค.

 

๋จน์„ ๋ฌผ๊ณ ๊ธฐ๊ฐ€ ์žˆ์—ˆ๋‹ค๋ฉด pq์— ์ €์žฅ๋˜์–ด for๋ฌธ ์•„๋ž˜ if๋ฌธ์ด ์‹คํ–‰๋ ํ…๋ฐ pq์— ๋จน์„ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋จน๊ณ  

๊ทธ ์œ„์น˜์—์„œ ๋‹ค์‹œ bfs๋ฅผ ํ†ตํ•ด ์ƒ์–ด๋ฅผ ์›€์ง์ด๊ณ  ๋จน์„ ๋ฌผ๊ณ ๊ธฐ๋“ค์„ pq์— ๋„ฃ์–ด ์›€์ง์ผ ์ˆ˜ ์žˆ๋‹ค.

 

์ด ๋ฌธ์ œ์˜ ํ•ต์‹ฌ์€ pq์— ๋ฌผ๊ณ ๊ธฐ๋“ค์„ ๋จน์„ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•œ ์šฐ์„  ์ˆœ์œ„ ํ๋ฅผ ์ด์šฉํ•œ๋ฐ์— ์žˆ๋Š”๊ฑฐ ๊ฐ™๋‹ค.

๋ฌธ์ œ ์กฐ๊ฑด์—์„œ ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ€๊น๋‹ค๋ฉด ๊ฐ€์žฅ ์œ„์— ์žˆ๋Š” ๋ฌผ๊ณ ๊ธฐ ์ฆ‰ y๊ฐ’์ด ์ž‘์€ ๋ฌผ๊ณ ๊ธฐ๋ฅผ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์ด๊ณ  ๊ฐ€์žฅ ์™ผ์ชฝ ๋ฌผ๊ณ ๊ธฐ๋Š” x๊ฐ’์ด ์ž‘์€ ๋ฌผ๊ณ ๊ธฐ๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ๋”ฐ๋ผ์„œ cmp ํ•จ์ˆ˜์—์„œ ๊ฑฐ๋ฆฌ, y ์ขŒํ‘œ, x ์ขŒํ‘œ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ๋ฌผ๊ณ ๊ธฐ ์šฐ์„  ์ˆœ์œ„๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค.

 

๋งŒ์•ฝ q์˜ ์›€์ง์ž„์ด ๋๋‚ฌ๋Š”๋ฐ ๋จน์„ ๋ฌผ๊ณ ๊ธฐ๊ฐ€ ์—†๋‹ค๋ฉด ์—„๋งˆ ์ƒ์–ด๊ฐ€ ์ถœ๋™ํ•ด์•ผ ํ•  ์ฐจ๋ก€์ด๋ฏ€๋กœ ์—ฌํƒœ ์›€์ง์ธ dist๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  while๋ฌธ์„ ๋น ์ ธ๋‚˜์˜จ๋‹ค!

 

c++ ์ฝ”๋“œ :

//
//  16236_babyShark.cpp
//  SOMA๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป
//
//  Created by JoSoJeong on 2021/05/05.
//

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <string.h>

using namespace std;
int arr[21][21];
int n;
int visit[21][21];
int dx[] = { 0, 0, 1, -1 }; //up down left right
int dy[] = { 1, -1, 0, 0 };

using namespace std;

struct fish
{
    int dist;
    int y;
    int x;
};

struct compare
{
    bool operator()(fish a, fish b)
    {
        if (a.dist == b.dist)
        {
            if (a.y == b.y)
            {
                return a.x > b.x;
            }
            return a.y > b.y;
        }
        return a.dist > b.dist;
    }
};

void eat_fish_bfs(pair<int, int> start){
    queue<fish> q;
    q.push({0, start.first, start.second});
    
    int weight = 2; //ํ˜„์žฌ ์ƒ์–ด ๋ชธ๋ฌด๊ฒŒ
    int dist = 0; // ์ด๋™ ๊ฑฐ๋ฆฌ
    int eatCount = 0; // ๋จน์€ ๋ฌผ๊ณ ๊ธฐ ์ˆ˜
    
    while(true){
        priority_queue<fish, vector<fish>, compare> pq; // ์กฐ๊ฑด์— ๋งž๊ฒŒ ๋ฌผ๊ณ ๊ธฐ๋ฅผ ์ •๋ ฌํ•œ ์šฐ์„  ์ˆœ์œ„ ํ
        
        while(!q.empty()){
            int x = q.front().x;
            int y = q.front().y;
            int curDist = q.front().dist;
            q.pop();
            for(int i = 0; i <4; i++){
                int nx = x + dx[i];
                int ny = y + dy[i];
                if(nx < 0 || ny < 0 || nx >= n || ny >= n) continue;
                if(arr[ny][nx] > weight || visit[ny][nx]) continue;
                q.push({curDist + 1, ny, nx});
                visit[ny][nx] = true;
                
                if(arr[ny][nx] < weight && arr[ny][nx] != 0){
                    pq.push({curDist + 1, ny, nx});
                }
            }
        }
        memset(visit, false, sizeof(visit));
        if(!pq.empty()){ // ๋จน์„ ๋ฌผ๊ณ ๊ธฐ ๋‚จ์•„ ์žˆ๋‹ค๋ฉด
            int fishX = pq.top().x;
            int fishY = pq.top().y;
            int fishDist = pq.top().dist;
            pq.pop();
            
            arr[fishY][fishX] = 0;
            eatCount++;
            dist = fishDist;
            q.push({dist, fishY, fishX}); //์ƒ์–ด ์›€์ง์ž„
            visit[fishY][fishX] = true;
        }else{
            cout << dist << '\n';
            break;
        }
        
        if(eatCount == weight){
            weight++;
            eatCount = 0;
        }
    }
    
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    
    pair<int, int> start;
    cin >> n;
    for(int i = 0 ; i < n; i++){
        for(int j = 0 ; j < n; j++){
            cin >> arr[i][j];
            if(arr[i][j] == 9){
                start = {i, j};
                arr[i][j] = 0;
                visit[i][j] = true;
            }
        }
    }
    
    eat_fish_bfs(start);
    
    return 0;
}