프로그래밍/문제풀이

[bfs] 백준 2468 안전 영역

하용권 2018. 11. 13. 22:13

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


이번 건 많이 쉬웠습니다. 딱히 설명 할 것이 없네요.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
int map[101][101= {0};
int dx[4= {1,-1,0,0};
int dy[4= {0,0,1,-1};
int bfs(int N, int rain);
 
 
 
int main(){
    ios_base::sync_with_stdio(false);
    int N,x,y;
    int ret=0, max_height=0;
    cin >> N;
    
    for(int i = 0 ; i < N; i++){
        for(int j = 0; j < N; j++){
            cin >> map[i][j];
            max_height = max(max_height, map[i][j]);
        }
    }
    
    for(int i = 0; i <= max_height; i++){
        ret = max(ret,bfs(N, i));
    }
    cout << ret;
    
    
    
}
 
int bfs(int N, int rain){
    bool visited[100][100]= {0};
    int count = 0;
    queue<pair<int,int>> q; //y,x
    
    
    for(int i = 0; i < N; i++){
        for(int j = 0; j <N; j++){
            
            if(!visited[i][j] && rain <= map[i][j]){
                q.push({i,j});
                count +=1;
                visited[i][j] = 1;
                
                while(!q.empty()){
                
                    for(int k=0;k<4;k++){
                        int y = q.front().first+dy[k];
                        int x = q.front().second + dx[k];
                        if(x <0 || x > N-1 || y < 0 || y > N-1continue;
                        if(!visited[y][x] && rain <= map[y][x]){
                            visited[y][x] = 1; q.push({y,x});
                        }
                    }
                    q.pop();
                    
                }
                
                
            }
            
        }
    }
    
    return count;
}
cs


반응형