-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy path193A. Cutting Figure.cpp
60 lines (51 loc) · 1.35 KB
/
193A. Cutting Figure.cpp
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
#include <bits/stdc++.h>
using namespace std;
int const N = 51;
int n, m;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
char s[N][N];
bool vis[N][N];
void DFS(int i, int j) {
vis[i][j] = true;
for(int k = 0, nx, ny; k < 4; ++k) {
nx = i + dx[k];
ny = j + dy[k];
if(nx < 0 || nx >= n || ny < 0 || ny >= m || vis[nx][ny] || s[nx][ny] == '.')
continue;
DFS(nx, ny);
}
}
int main() {
scanf("%d %d", &n, &m);
for(int i = 0; i < n; ++i)
scanf("%s", s[i]);
int tmp = 0;
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
tmp += (s[i][j] == '#');
if(tmp <= 2) {
puts("-1");
return 0;
}
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
if(s[i][j] == '#') {
memset(vis, false, sizeof vis);
s[i][j] = '.';
int comp = 0;
for(int k = 0; k < n; ++k)
for(int l = 0; l < m; ++l)
if(!vis[k][l] && s[k][l] == '#') {
++comp;
DFS(k, l);
}
if(comp > 1) {
puts("1");
return 0;
}
s[i][j] = '#';
}
puts("2");
return 0;
}