๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/๋ฐฑ์ค€(BOJ)

[๋ฐฑ์ค€,c++] 16236๋ฒˆ - ์•„๊ธฐ ์ƒ์–ด

by ์•ˆ์ฃผํ˜• 2022. 8. 28.

๋ฌธ์ œ

 

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

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

www.acmicpc.net

 

์ฝ”๋“œ

#include <iostream>
#include <vector>
#include <queue>
#include <memory.h>

using namespace std;

int N, _time;
int map[21][21], cost[21][21];
int shark_r, shark_c, shark_size, now_eat_cnt;
int dx[4] = {0,0,-1,1};
int dy[4] = {-1,1,0,0};
bool visited[21][21];

pair<int,int> find_near_fish_coor(){ // ํ˜„์žฌ ์ƒ์–ด์œ„์น˜์—์„œ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๋ฌผ๊ณ ๊ธฐ์˜ ์ขŒํ‘œ๋ฅผ ์ฐพ์Œ
    int min_dist = 1e9;
    int fish_r = 1e9, fish_c = 1e9;
    for(int i=0; i<N; i++){
        for(int k=0; k<N; k++){
            if(map[i][k] !=9 && map[i][k]!=0 && map[i][k]<shark_size && cost[i][k] != 0){
                int dist = cost[i][k];
                if(dist == min_dist){   // ๋ฌผ๊ณ ๊ธฐ์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ™์„ ๋•Œ
                    if(i == fish_r){    // ๋ฌผ๊ณ ๊ธฐ์˜ ๋†’์ด๊ฐ€ ๊ฐ™์„ ๋•Œ
                        if(k <= fish_c){    // ๊ฐ€์žฅ ์™ผ์ชฝ์— ์žˆ๋Š” ๋ฌผ๊ณ ๊ธฐ๋ฅผ ๊ณ ๋ฆ„
                            fish_r = i;
                            fish_c = k;
                            min_dist = dist;
                        }
                    }
                    else if(i<fish_r){
                        fish_r = i;
                        fish_c = k;
                        min_dist = dist;
                    }
                }
                else if(dist<min_dist){
                    fish_r = i;
                    fish_c = k;
                    min_dist = dist;
                }
            }
        }
    }
    return {fish_r, fish_c};
}

void cal_dist(int r, int c){
    visited[r][c] = 1;
    queue<pair<int,int>> q;
    q.push({r,c});
    
    while(!q.empty()){
        int x = q.front().first;
        int y = q.front().second;
        q.pop();
        for(int i=0; i<4; i++){
            int nx = x + dx[i];
            int ny = y + dy[i];
            if(nx>=0 && nx<N && ny>=0 && ny<N){
                if(!visited[nx][ny] && shark_size>=map[nx][ny]){ // ์•„๊ธฐ ์ƒ์–ด๋Š” ์ž์‹ ๊ณผ ํฌ๊ธฐ๊ฐ€ ๊ฐ™๊ฑฐ๋‚˜ ์ž‘์€ ๋ฌผ๊ณ ๊ธฐ๋งŒ ์ง€๋‚˜๊ฐˆ ์ˆ˜ ์žˆ์Œ
                    visited[nx][ny] = 1;
                    cost[nx][ny] = cost[x][y] + 1;
                    q.push({nx, ny});
                }
            }
        }
    }
}

bool move_and_eat_fish(pair<int,int> fish_coor){
    int x = fish_coor.first;
    int y = fish_coor.second;
    if(x == 1e9 || y == 1e9) return false;  // ์ƒ์–ด๋ณด๋‹ค ๋ฉ์น˜๊ฐ€ ์ž‘์€ ๋ฌผ๊ณ ๊ธฐ๊ฐ€ ์žˆ์ง€๋งŒ ๋ฉ์น˜๊ฐ€ ํฐ ๋ฌผ๊ณ ๊ธฐ๋“ค ๋•Œ๋ฌธ์— ๋จน์œผ๋Ÿฌ ๊ฐ€์ง€ ๋ชปํ•  ๊ฒฝ์šฐ
    _time += cost[x][y];
    map[shark_r][shark_c] = 0;
    shark_r = x;
    shark_c = y;
    map[shark_r][shark_c] = 9;
    now_eat_cnt++;
    if(now_eat_cnt == shark_size){
        shark_size++;
        now_eat_cnt = 0;
    }
    return true;
}

bool isfish(){  // ๋จน์„ ์ˆ˜ ์žˆ๋Š”๋ฌผ๊ณ ๊ธฐ๊ฐ€ ๋‚จ์•„์žˆ๋Š”์ง€ ํ™•์ธ
    for(int i=0; i<N; i++){
        for(int k=0; k<N; k++){
            if(map[i][k] == 9) continue;
            if(map[i][k]!=0 && map[i][k] < shark_size) return true;
        }
    }
    return false;
}

void simulation(){
    while(isfish()){
        cal_dist(shark_r, shark_c);
        if(!move_and_eat_fish(find_near_fish_coor())) return;
        memset(visited, 0 ,sizeof(visited));
        memset(cost, 0, sizeof(cost));
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    cin>>N;
    
    for(int i=0; i<N; i++){
        for(int k=0; k<N; k++){
            cin>>map[i][k];
            if(map[i][k] == 9){
                shark_r = i;
                shark_c = k;
                shark_size = 2;
            }
        }
    }
    simulation();
    cout<<_time;
}

 

ํ’€์ด

  1. bfs๋ฅผ ํ†ตํ•ด์„œ ๊ฐ ๋ฌผ๊ณ ๊ธฐ๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. (์ด๋•Œ ์ƒ์–ด๋Š” ์ž์‹ ๋ณด๋‹ค ํฌ๊ธฐ๊ฐ€ ๊ฐ™๊ฑฐ๋‚˜ ์ž‘์€ ๋ฌผ๊ณ ๊ธฐ๋งŒ ์ง€๋‚  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.)
  2. ๊ทธ ํ›„, ํ˜„์žฌ ์ƒ์–ด์˜ ์œ„์น˜์—์„œ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๋ฌผ๊ณ ๊ธฐ์˜ ์ขŒํ‘œ๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.(์ด๋•Œ ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ™๋‹ค๋ฉด ์กฐ๊ฑด ์šฐ์„ ์ˆœ์œ„์— ๋งž๋Š” ์ขŒํ‘œ๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.)
  3. ์ด์ œ ์ƒ์–ด๋ฅผ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๋ฌผ๊ณ ๊ธฐ๊นŒ์ง€์˜ ์ขŒํ‘œ๋กœ ์ด๋™์‹œํ‚ต๋‹ˆ๋‹ค. ์ด๋•Œ ์•„๋ž˜์™€ ๊ฐ™์ด ํฐ ๋ฌผ๊ณ ๊ธฐ๋“ค๋กœ ๋‘˜๋Ÿฌ์‹ธ์ธ ์ž…๋ ฅ์ผ ๊ฒฝ์šฐ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๋ฌผ๊ณ ๊ธฐ์˜ ์ขŒํ‘œ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ”๋กœ ์ข…๋ฃŒ์‹œํ‚ต๋‹ˆ๋‹ค.

2
3 9
1 3

๊ทธ ํ›„ ์ƒ์–ด๊ฐ€ ๋จน์„ ์ˆ˜ ์žˆ๋Š” ๋ฌผ๊ณ ๊ธฐ๊ฐ€ ๋‚˜์˜ฌ ๋•Œ ๊นŒ์ง€ ์œ„ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.

๋Œ“๊ธ€