반응형

문제 출처 :


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



알고리즘 분석 :


문제 해결에 필요한 사항

1. 구현


이 문제는 보통 재귀로 푸는데 재귀가 아닌 3중 for문으로 해결 할 수 있다.


자세한 내용은 주석을 통해 달아두었다.







소스 코드 : 


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
114
115
116
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <memory.h>
#include <vector>
#include <queue>
 
using namespace std;
 
typedef pair<intint> pii;
 
int n, m, idx;
pii map[102];
vector<pii> virus;
 
int dy[4= { -1,0,1,};
int dx[4= { 0,-1,0,};
 
int bfs(int tmp[][10])
{
    // 내가 받은 배열을 arr에 복사해준다.
    int arr[10][10];
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            arr[i][j] = tmp[i][j];
 
    bool visit[10][10= { false, };
    queue<pii> q;
 
    // 바이러스를 먼저 queue에 넣어 바이러스가 퍼지도록 한다.
    for (int i = 0; i < virus.size(); i++)
        q.push({ virus[i] });
 
    while (!q.empty())
    {
        int y = q.front().first;
        int x = q.front().second;
 
        q.pop();
 
        if (visit[y][x])
            continue;
 
        visit[y][x] = true;
        arr[y][x] = 2;
 
        for (int i = 0; i < 4; i++)
        {
            int ny = y + dy[i];
            int nx = x + dx[i];
 
            if ((<= ny && ny < n) && (<= nx && nx < m) && arr[ny][nx] == 0)
                q.push({ ny,nx });
        }
    }
 
    // 바이러스가 퍼지고 0인 부분을 관찰한다.
    int safe = 0;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            if (!arr[i][j])
                safe++;
 
    return safe;
}
int main()
{
    int arr[10][10];
    memset(arr, 0sizeof(arr));
 
    scanf("%d %d"&n, &m);
 
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
        {
            scanf("%d"&arr[i][j]);
            map[idx++= { i,j }; // 각 좌표를 인덱스 번호로 생각해본다.
            // (0,0)은 0번, (0,1)은 1번 ... 
 
            // 바이러스는 벡터에 담아준다.
            if (arr[i][j] == 2)
                virus.push_back({ i,j });
        }
 
    int ans = 0;
    // 세 점을 뽑는다
    for (int c1 = 0; c1 < idx - 2; c1++)
        for (int c2 = c1 + 1; c2 < idx - 1; c2++)
            for (int c3 = c2 + 1; c3 < idx; c3++)
            {
                int y1 = map[c1].first, x1 = map[c1].second;
                int y2 = map[c2].first, x2 = map[c2].second;
                int y3 = map[c3].first, x3 = map[c3].second;
                // 세 점이 모두 0이라면
                if (!arr[y1][x1] && !arr[y2][x2] && !arr[y3][x3])
                {
                    // 막대기로 변환시키고
                    arr[y1][x1] = arr[y2][x2] = arr[y3][x3] = 1;
 
                    // bfs를 돌려 0의 개수를 리턴받는다.
                    int ret = bfs(arr);
 
                    ans = max(ans, ret);
 
                    // 다시 0으로 만든다.
                    arr[y1][x1] = arr[y2][x2] = arr[y3][x3] = 0;
                }
            }
 
    printf("%d", ans);
 
    return 0;
}
 
//                                                       This source code Copyright belongs to Crocus
//                                                        If you want to see more? click here >>
Crocus

반응형

'Applied > 알고리즘 문제풀이' 카테고리의 다른 글

[14501번] 퇴사  (0) 2018.04.12
[3190번] 뱀  (0) 2018.04.09
[1633번] 최고의 팀 만들기  (0) 2018.04.03
[1613번] 역사  (0) 2018.03.28
[14428번] 수열과 쿼리 16  (0) 2018.03.25