๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm๐Ÿฐ/๋ฐฑ์ค€

[๋ฐฑ์ค€] 7576 ํ† ๋งˆํ†  BFS

by Jouureee 2021. 12. 10.

๋ฌธ์ œ :

https://www.acmicpc.net/problem/7576

 

7576๋ฒˆ: ํ† ๋งˆํ† 

์ฒซ ์ค„์—๋Š” ์ƒ์ž์˜ ํฌ๊ธฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋‘ ์ •์ˆ˜ M,N์ด ์ฃผ์–ด์ง„๋‹ค. M์€ ์ƒ์ž์˜ ๊ฐ€๋กœ ์นธ์˜ ์ˆ˜, N์€ ์ƒ์ž์˜ ์„ธ๋กœ ์นธ์˜ ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. ๋‹จ, 2 ≤ M,N ≤ 1,000 ์ด๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ๋Š” ํ•˜๋‚˜์˜ ์ƒ์ž์— ์ €์žฅ๋œ ํ† ๋งˆํ† 

www.acmicpc.net

 

๋ถ„์„ :

์ด๋Ÿฐ์‹์œผ๋กœ ํ† ๋งˆํ†  ๋†์žฅ์ด ์กด์žฌ ํ• ๋•Œ 

1์€ ์ต์€ ํ† ๋งˆํ† , ์ •์ˆ˜ 0์€ ์ต์ง€ ์•Š์€ ํ† ๋งˆํ† , ์ •์ˆ˜ -1์€ ํ† ๋งˆํ† ๊ฐ€ ๋“ค์–ด์žˆ์ง€ ์•Š์€ ์นธ

์„ ์˜๋ฏธํ•œ๋‹ค.

 

์ต์€ ํ† ๋งˆํ†  ์ค‘์‹ฌ์œผ๋กœ ์ƒํ•˜์ขŒ์šฐ๋กœ ํ•˜๋ฃจ๋งˆ๋‹ค ํ† ๋งˆํ† ๊ฐ€ ์ต๋Š”๋ฐ ๋ช‡์ผ์ด๋ฉด ๋†์žฅ ๋‚ด ๋ชจ๋“  ํ† ๋งˆํ† ๊ฐ€ ์ต๋Š”์ง€ ๋‚ ์งœ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์˜€๋‹ค.

 

์šฐ์„  ํ† ๋งˆํ†  ๋†์žฅ์„ ํƒ์ƒ‰ํ•œ๋‹ค๋Š” ์ ์—์„œ BFS, DFS๋กœ ์ ‘๊ทผํ•ด์•ผ ํ•œ๋‹ค ์ƒ๊ฐํ–ˆ๊ณ  ํƒ์ƒ‰์„ ํ•˜๋ฉด์„œ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋˜  0์„ for๋ฌธ์„ ๋Œ๋ฉด์„œ 1์”ฉ ๋Š˜๋ ค ์ฃผ๊ณ  ๋ชจ๋“  0์ด 1๋กœ ๋ฐ”๋€Œ์—ˆ์„๋•Œ dfs ํƒ์ƒ‰์„ ํ†ตํ•ด ๋‚ ์งœ๋ฅผ ๊ณ„์‚ฐํ•˜๋„๋ก ํ•˜์˜€๋‹ค. 

 

//
//  7576_tomato.cpp
//  SOMA๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป
//
//  Created by JoSoJeong on 2021/04/22.
//

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

using namespace std;
int tomatoMap[1001][1001];
int M, N;
int tomatoCount;
int dfscount;
int visit[1001][1001] = {false, };

int dx[] = { 0, 0, 1, -1 }; //up down left right
int dy[] = { 1, -1, 0, 0 };

queue<pair<int, int>> s;

void tomato_dfs(){
    while(!s.empty()){
        pair<int, int> cur = s.front();
        s.pop();
       
        for(int i = 0; i < 4; i++){
            int nextX = cur.first + dx[i];
            int nextY = cur.second + dy[i];
           
            if(nextX < N && nextY < M && nextX >= 0 && nextY >= 0 ){
                if(visit[nextX][nextY] == false && tomatoMap[nextX][nextY] == 0){
                    visit[nextX][nextY] = true;
                    s.push({nextX, nextY});
                    dfscount++;
                    tomatoMap[nextX][nextY] = tomatoMap[cur.first][cur.second] + 1;
                }
            }
        }

    }
}

int main(){
    cin >> M >> N; // 6 4
    for(int i = 0; i < N; i++){
        for(int j = 0; j < M; j++){
            cin >> tomatoMap[i][j];
            if(tomatoMap[i][j] == 0){ // tomato count
                tomatoCount++;
            }else if(tomatoMap[i][j] == 1){
                s.push({i,j});
                //startPoint = make_pair(i,j);
            }
        }
    }
    int max = 0;
    if(tomatoCount == 0){
        cout << "0" << '\n'; // ๋ชจ๋“  ํ† ๋งˆํ†  ์ต์–ด์žˆ๋Š” ์ƒํƒœ
        return 0;
    }else{
        tomato_dfs();
        if(tomatoCount - dfscount != 0){ // ํ† ๋งˆํ†  ๋‹ค ์ต์ง€ x
            cout << "-1" << '\n';
            return 0;
        }
        for(int i = 0; i < N; i++){ // ํ† ๋งˆํ†  ๋‹ค ์ตํž˜
            for(int j = 0; j < M; j++){
                if(max < tomatoMap[i][j]){
                    max = tomatoMap[i][j];
                }
                    
            }
        }
        cout << max - 1 << '\n';

    }
    return 0;
}

๋งˆ์ง€๋ง‰ ํ† ๋งˆํ†  ์ต์€ ๋‚ ์งœ๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ์ด์ค‘ for๋ฌธ์œผ๋กœ max ๊ฐ’์„ ์ฐพ๋Š” ๋ถ€๋ถ„์ด ๋น„ํšจ์œจ์ ์œผ๋กœ ๋ณด์ธ๋‹ค. ์ด๋ถ€๋ถ„์€ ์ˆ˜์ •ํ•ด๋ณด์•„์•ผ๊ฒ ๋‹ค.

๋Œ“๊ธ€