출처: https://bumcrush.tistory.com/182 [맑음때때로 여름]

상세 컨텐츠

본문 제목

백준 2468 안전 영역(dfs)

알고리즘

by 장동균 2020. 7. 23. 01:00

본문

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

 

2468번: 안전 영역

재난방재청에서는 많은 비가 내리는 장마철에 대비해서 다음과 같은 일을 계획하고 있다. 먼저 어떤 지역의 높이 정보를 파악한다. 그 다음에 그 지역에 많은 비가 내렸을 때 물에 잠기지 않는 �

www.acmicpc.net

#include <iostream>

using namespace std;
const int MAX = 100;
typedef struct
{
    int x, y;
} Dir;
int N;
int graph[MAX][MAX];
int cnt;
bool visited[MAX][MAX];
Dir dirMove[4] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
int result;

void dfs(int x, int y, int depthNum)
{
    visited[x][y] = true;
    for (int i = 0; i < 4; i++)
    {
        int nextX = x + dirMove[i].x;
        int nextY = y + dirMove[i].y;
        if (nextX >= 0 && nextX < N && nextY >= 0 && nextY < N && !visited[nextX][nextY] && graph[nextX][nextY] >= depthNum)
        {
            dfs(nextX, nextY, depthNum);
        }
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> N;
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
            cin >> graph[i][j];

    for (int depth = 1; depth <= 100; depth++)
    {
        cnt = 0;
        for (int i = 0; i < N; i++)
            for (int j = 0; j < N; j++)
                visited[i][j] = false;

        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++)
            {
                if (graph[i][j] >= depth && !visited[i][j])
                {
                    cnt++;
                    dfs(i, j, depth);
                }
            }
        }
        result = max(result, cnt);
    }
    cout << result;
}

기본적인 dfs 문제라 크게 어렵지 않았다.

 

제일 어려웠던건 문제에 대한 이해........

처음 몇번 읽고 뭔 헛소리인가 했다.

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

백준 1987 알파벳(dfs)  (0) 2020.07.25
백준 2583 영역 구하기(dfs)  (0) 2020.07.25
백준 14502 연구소(dfs)  (0) 2020.07.23
BFS, DFS  (0) 2020.07.19
백준 1748 수 이어 쓰기 1(brute force)  (0) 2020.07.18

관련글 더보기

댓글 영역