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

상세 컨텐츠

본문 제목

백준 14502 연구소(dfs)

알고리즘

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

본문

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

 

14502번: 연구소

인체에 치명적인 바이러스를 연구하던 연구소에서 바이러스가 유출되었다. 다행히 바이러스는 아직 퍼지지 않았고, 바이러스의 확산을 막기 위해서 연구소에 벽을 세우려고 한다. 연구소는 크�

www.acmicpc.net

#include <iostream>

using namespace std;

typedef struct
{
    int x, y;
} Dir;
Dir DirMove[4] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};

const int MAX = 8;
int graph[MAX][MAX];
int tmpGraph[MAX][MAX];
bool visited[MAX][MAX];
int N, M;
int result;
int dfsGraph[MAX][MAX];
int cnt;

void dfs(int x, int y)
{
    visited[x][y] = 1;
    for (int n = 0; n < 4; n++)
    {
        int nextX = x + DirMove[n].x;
        int nextY = y + DirMove[n].y;
        if (0 <= nextX && nextX < N && 0 <= nextY && nextY < M && dfsGraph[nextX][nextY] == 0 && !visited[nextX][nextY])
        {
            visited[nextX][nextY] = 1;
            dfsGraph[nextX][nextY] = 2;
            dfs(nextX, nextY);
        }
    }
}

void checkWall(int x)
{
    if (x == 3)
    {

        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < M; j++)
            {
                dfsGraph[i][j] = tmpGraph[i][j];
                visited[i][j] = 0;
            }
        }

        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < M; j++)
            {
                if (dfsGraph[i][j] == 2 && !visited[i][j])
                {
                    visited[i][j] = 1;
                    dfs(i, j);
                }
            }
        }

        cnt = 0;
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < M; j++)
            {
                if (dfsGraph[i][j] == 0)
                    cnt++;
            }
        }
        result = max(result, cnt);

        return;
    }

    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < M; j++)
        {
            if (tmpGraph[i][j] == 0)
            {
                tmpGraph[i][j] = 1;
                checkWall(x + 1);
                tmpGraph[i][j] = 0;
            }
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> N >> M;
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < M; j++)
        {
            cin >> graph[i][j];
        }
    }
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < M; j++)
        {
            if (graph[i][j] == 0)
            {
                for (int k = 0; k < N; k++)
                {
                    for (int l = 0; l < M; l++)
                    {
                        tmpGraph[k][l] = graph[k][l];
                    }
                }
                tmpGraph[i][j] = 1;
                checkWall(1);
                tmpGraph[i][j] = 0;
            }
        }
    }
    cout << result;
}

 

먼저, https://jaimemin.tistory.com/search/14502

 

'14502'의 검색결과

소소한 프로그래밍과 약간의 게임

jaimemin.tistory.com

이곳의 코드를 참고했다. 이 코드를 충분히 이해한 이후 내 나름대로 dfs 방식으로 풀어야겠다는 생각을 가졌고 결국 풀긴 했지만 시간이 꽤 걸렸다.....

 

parameter의 개수를 조금 늘려서 for loop 도는 횟수를 크게 줄일 수 있을 것 같긴한데 귀찮아서 그렇게까지는 하지 않았다. 이 코드에서 사용한 벽을 세우고 ----> 함수 호출 ----> 벽 제거 이 개념을 잘 기억해둘 필요가 있다.

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

백준 2583 영역 구하기(dfs)  (0) 2020.07.25
백준 2468 안전 영역(dfs)  (0) 2020.07.23
BFS, DFS  (0) 2020.07.19
백준 1748 수 이어 쓰기 1(brute force)  (0) 2020.07.18
백준 1966 프린터 큐(brute force)  (0) 2020.07.18

관련글 더보기

댓글 영역