Algorithm🐰/λ°±μ€€

[λ°±μ€€] 7576 ν† λ§ˆν†  BFS

Jouureee 2021. 12. 10. 16:44

문제 :

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 값을 μ°ΎλŠ” 뢀뢄이 λΉ„νš¨μœ¨μ μœΌλ‘œ 보인닀. 이뢀뢄은 μˆ˜μ •ν•΄λ³΄μ•„μ•Όκ² λ‹€.