ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 안전영역
    백준알고리즘 2019. 7. 20. 03:29
    
    #include<iostream>
    #include<cstdio>
    #include<queue>
    #include<utility>
    
    using namespace std;
    
    int N;
    int a[100][100];
    bool d[100][100];
    int dx[] = { 0,0,1,-1 };
    int dy[] = { 1,-1,0,0 };
    int MAX;
    
    void bfs(int x, int y, int h) {
    	queue<pair<int, int>> q;
    	q.push(make_pair(x, y));
    	d[x][y] = true;
    	while (!q.empty()) {
    		x = q.front().first; 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 < N && nx >= 0 && ny < N && ny >= 0) {
    				if (d[nx][ny] == false) {
    					if (a[nx][ny] > h)
    					{
    						q.push(make_pair(nx, ny));
    						d[nx][ny] = true;
    					}
    				}
    			}
    		}
    	}
    }
    
    void initial() {
    	for (int i = 0; i < N; i++) {
    		for (int j = 0; j < N; j++) {
    			d[i][j] = false;
    		}
    	}
    }
    
    bool check(int no) {
    	int alp = 0;
    	for (int i = 0; i < N; i++) {
    		for (int j = 0; j < N; j++) {
    			if (a[i][j] <= no) {
    				alp += 1;
    			}
    		}
    	}
    	if (alp == N * N) return false;
    	else return true;
    }
    
    int main() {
    	scanf("%d", &N);
    	for (int i = 0; i < N; i++) {
    		for (int j = 0; j < N; j++) {
    			scanf("%d", &a[i][j]);
    		}
    	}
    
    	for (int i = 0; i <= 100; i++) {
    		int cnt = 0;
    		int height = i;
    		for (int j = 0; j < N; j++) {
    			for (int k = 0; k < N; k++) {
    				if (d[j][k] == false) {
    					if (a[j][k] > height) {
    						bfs(j, k, height);
    						cnt++;
    						
    					}
    				}
    			}
    		}
    		initial();
    		if (cnt > MAX) MAX = cnt;
    	}
    	printf("%d\n", MAX);
    
    	return 0;
    }

    졸려서그런지 조건하나빼먹어서 런타임에러만 몇번을 했는지.. 전형적인 BFS문제

    '백준알고리즘' 카테고리의 다른 글

    퇴사  (0) 2019.07.21
    테트로미노  (0) 2019.07.21
    인구 이동  (0) 2019.07.17
    영역 구하기  (0) 2019.07.17
    보물섬  (0) 2019.07.17

    댓글

Designed by Tistory.