SDJ( 수돈재 아님 ㅎ )

[C++] 14502 - 연구소 본문

알고리즘/Backjoon

[C++] 14502 - 연구소

ShinDongJun 2020. 1. 29. 09:22

문제 링크 : https://www.acmicpc.net/problem/14502

 

14502번: 연구소

인체에 치명적인 바이러스를 연구하던 연구소에서 바이러스가 유출되었다. 다행히 바이러스는 아직 퍼지지 않았고, 바이러스의 확산을 막기 위해서 연구소에 벽을 세우려고 한다. 연구소는 크기가 N×M인 직사각형으로 나타낼 수 있으며, 직사각형은 1×1 크기의 정사각형으로 나누어져 있다. 연구소는 빈 칸, 벽으로 이루어져 있으며, 벽은 칸 하나를 가득 차지한다.  일부 칸은 바이러스가 존재하며, 이 바이러스는 상하좌우로 인접한 빈 칸으로 모두 퍼져나갈 수 있다.

www.acmicpc.net

 

 

 

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include<bits/stdc++.h>
 
#define endl '\n'
 
using namespace std;
 
int N, M;
 
int save;
int cnt;
 
int board[10][10];
int copy_board[10][10];
int visited[10][10];
 
int dx[4= {001-1};
int dy[4= {1-100};
 
void search();
void DFS(int y, int x);
 
int main(void)
{
    cin >> N >> M;
    for(int i =0 ; i < N; ++i)
    {
        for(int j = 0; j < M; ++j)
        {
            cin >> board[i][j];
        }
    }
 
    for(int i = 0; i < N*M-2++i)
    {
        if(board[i/M][i%M] != 0)
            continue;
        board[i/M][i%M] = 1;
        for(int j = i+1; j < N*M-1++j)
        {
            if(board[j/M][j%M] != 0)
                continue;
 
            board[j/M][j%M] = 1;
            for(int z = j+1; z < N*M; ++z)
            {
                if(board[z/M][z%M] != 0)
                    continue;
 
                board[z/M][z%M] = 1;
                search();
                board[z/M][z%M] = 0;
                memset(visited, 0sizeof(visited));
                memset(copy_board, 0sizeof(copy_board));
            }
            board[j/M][j%M] = 0;
        }
        board[i/M][i%M] = 0;
    }
 
    cout << save << endl;
 
    return 0;
}
 
void search()
{
    memcpy(copy_board, board, sizeof(copy_board));
 
    for(int i = 0; i < N; ++i)
    {
        for(int j = 0; j < M; ++j)
        {
            if(copy_board[i][j] == 2)
            {
                DFS(i, j);
            }
        }
    }
 
    int cnt = 0;
    for(int i = 0; i < N; ++i)
    {
        for(int j = 0; j < M; ++j)
        {
            if(!copy_board[i][j])
                cnt++;
        }
    }
 
    if(save < cnt)
        save = cnt;    
}
 
void DFS(int y, int x)
{
    visited[y][x] = 1;
    copy_board[y][x] = 2;
 
    int nx, ny;
 
    for(int i = 0; i < 4++i)
    {
        ny = y + dy[i];
        nx = x + dx[i];
 
        if(!(0 <= nx && nx < M && 0 <= ny && ny < N))
            continue;
        if(visited[ny][nx])
            continue;
        if(copy_board[ny][nx] == 0)
            DFS(ny, nx);
    }
}
Comments