[C언어]14502. 연구소

SW 업무 관련/백준

[C언어]14502. 연구소

WillBe_ 2018. 9. 17. 01:31

6중 for문을 써서 벽을 세운 다음, 각각에 대해 BFS로 바이러스 퍼트림.


그리고 안전지대를 찾으면 끝!



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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <stdio.h>
#include <string.h>
 
typedef struct
{
    int x;
    int y;
}point;
 
int dx[] = { 0,0,-1,1 };
int dy[] = { -1,1,0,0 };
int map[10][10];
int map_Copy[10][10];
int N, M, count;
point virus[10];
int MAX;
 
int CheckBound(int y, int x)
{
    if (0 > y || 0 > x || N <= y || M <= x)
        return 0;
 
    return 1;
}
 
void MapCopy()
{
    memset(map_Copy, 0sizeof(map_Copy));
 
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < M; j++)
        {
            map_Copy[i][j] = map[i][j];
        }
    }
}
 
int Count_Zero()
{
    int c = 0;
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < M; j++)
        {
            if (map_Copy[i][j] == 0)
            {
                c++;
            }
        }
    }
    return c;
}
 
int main()
{
    count = 0;
    scanf("%d %d"&N, &M);
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < M; j++)
        {
            scanf("%d"&map[i][j]);
 
            if (map[i][j] == 2)
            {
                virus[count].y = i;
                virus[count++].x = j;
            }
        }
    }
 
    MAX = -1;
    for (int a = 0; a < N; a++)
    {
        for (int b = 0; b < N; b++)
        {
            for (int c = 0; c < N; c++)
            {
                for (int a1 = 0; a1 < M; a1++)
                {
                    for (int b1 = 0; b1 < M; b1++)
                    {
                        for (int c1 = 0; c1 < M; c1++)
                        {
                            /////////////////////////////////////////////
                            MapCopy();
 
                            if (map_Copy[a][a1] != 0)
                                continue;
                            map_Copy[a][a1] = 1;
 
                            if (map_Copy[b][b1] != 0)
                                continue;
                            map_Copy[b][b1] = 1;
 
                            if (map_Copy[c][c1] != 0)
                                continue;
                            map_Copy[c][c1] = 1;
 
                            int visit[8][8= { 0, };
                            for (int i = 0; i < count; i++)
                            {
                                point Q[8 * 8= { 0, };
                                int Q_Front = -1, Q_Back = -1;
 
                                Q[++Q_Back].y = virus[i].y;
                                Q[Q_Back].x = virus[i].x;
                                visit[virus[i].y][virus[i].x] = 1;
 
                                while (Q_Front != Q_Back)
                                {
                                    int y = Q[++Q_Front].y;
                                    int x = Q[Q_Front].x;
 
                                    for (int j = 0; j < 4; j++)
                                    {
                                        int yy = y + dy[j];
                                        int xx = x + dx[j];
 
                                        if (!CheckBound(yy, xx))
                                            continue;
 
                                        if (visit[yy][xx] == 0 && map_Copy[yy][xx] == 0)
                                        {
                                            Q[++Q_Back].y = yy;
                                            Q[Q_Back].x = xx;
                                            visit[yy][xx] = 1;
                                            map_Copy[yy][xx] = 2;
                                        }
                                    }
                                }
                            }
 
                            int temp = Count_Zero();
                            if (temp > MAX)
                                MAX = temp;
                            ///////////////////////////////////////////////////////////////////////////
                        }
                    }
                }
            }
        }
    }
 
    printf("%d", MAX);
    return 0;
}
cs


'SW 업무 관련 > 백준' 카테고리의 다른 글

[C언어] 6593 상범 빌딩  (0) 2018.09.24
[C언어] 14502. 연구소(DFS)  (0) 2018.09.23
[C언어]14503. 로봇 청소기  (0) 2018.09.16
[C언어]2573. 빙산  (0) 2018.09.16
[C언어] 2015 오목.  (0) 2018.08.21