https://www.acmicpc.net/problem/14502
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
|
#include <iostream>
#include <vector>
int n, m;
int lab[10][10];
int myLab[10][10];
std::vector<std::pair<int, int>> covid19;
int ans = 0;
int dy[4] = { 0, 1, 0, -1 };
int dx[4] = { 1, 0, -1, 0 };
void print()
{
std::cout << '\n';
for (int y = 0; y <= n + 1; ++y)
{
for (int x = 0; x <= m + 1; ++x)
{
std::cout << myLab[y][x] << ' ';
}
std::cout << '\n';
}
std::cout << "------------------------------------\n";
}
void input()
{
std::cin >> n >> m;
for (int y = 1; y <= n; ++y)
{
for (int x = 1; x <= m; ++x)
{
std::cin >> lab[y][x];
if (lab[y][x] == 2)
{
covid19.push_back(std::pair<int, int>({ y, x }));
}
myLab[y][x] = lab[y][x];
}
}
}
void ff(int y, int x)
{
if (y < 1 || n < y || x < 1 || m < x)
{
return;
}
myLab[y][x] = 2;
for (int i = 0; i < 4; ++i)
{
if (myLab[y + dy[i]][x + dx[i]] == 0)
{
ff(y + dy[i], x + dx[i]);
}
}
}
void clear()
{
for (int y = 1; y <= n; ++y)
{
for (int x = 1; x <= m; ++x)
{
if (myLab[y][x] == 2)
{
myLab[y][x] = 0;
}
}
}
for (auto& it : covid19)
{
myLab[it.first][it.second] = 2;
}
}
void solve(int stk)
{
if (stk == 3)
{
//바이러스 플러드필
for (auto& it : covid19)
{
ff(it.first, it.second);
}
//print();
//남은 0 체크
int safety = 0;
for (int y = 1; y <= n; ++y)
{
for (int x = 1; x <= m; ++x)
{
if (myLab[y][x] == 0)
{
++safety;
}
}
}
ans = ans < safety ? safety : ans;
clear();
//print();
return;
}
//모든 0에 벽 3개 설치
for (int y = 1; y <= n; ++y)
{
for (int x = 1; x <= m; ++x)
{
if (myLab[y][x] == 0)
{
myLab[y][x] = 1;
//print();
solve(stk + 1);
myLab[y][x] = 0;
}
}
}
}
void output()
{
std::cout << ans;
}
int main()
{
input();
solve(0);
output();
return 0;
}
|
cs |
가로세로 바꿔써서 삽질한건 안자랑
'알고리즘' 카테고리의 다른 글
백준 6603 : 로또 (0) | 2020.06.23 |
---|---|
백준 14888 : 연산자 끼워넣기 (0) | 2020.06.23 |
백준 17478 : 재귀함수가 뭔가요? (0) | 2020.06.19 |
탐색공간의 배제 (0) | 2020.06.18 |
백준 14501 : 퇴사 (0) | 2020.06.18 |