-
Notifications
You must be signed in to change notification settings - Fork 0
/
uva352.cpp
71 lines (61 loc) · 1.33 KB
/
uva352.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
61
62
63
64
65
66
67
68
69
70
71
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<bitset>
using namespace std;
const int mx = 401;
int grid[mx][mx];
int vis[mx][mx];
int n;
bool safe(int i, int j) {
return (i<n && i>=0) && (j<n && j>=0) && (grid[i][j]==1);
}
void kill(int i, int j) {
if (vis[i][j] == 1) return;
if (!safe(i, j)) return ;
vis[i][j] = 1;
// cout << i << " " << j << endl;
kill(i-1, j-1);
kill(i-1, j);
kill(i-1, j+1);
kill(i, j-1);
kill(i, j+1);
kill(i+1, j-1);
kill(i+1, j);
kill(i+1, j+1);
return;
}
int solve() {
// memset(grid, -1, sizeof grid);
memset(vis, 0, sizeof vis);
string s;
// cout << " solving\n";
for (int i=0; i<n; i++) {
cin >> s;
for (int j=0; j<n; j++) {
grid[i][j] = s[j]-'0';
// cout << grid[i][j];
}
// cout << endl;
}
int ans=0;
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
if ((grid[i][j]==1) && (vis[i][j]==0)) {
ans++;
// cout << i << " " << j << endl;
kill(i, j);
}
}
}
return ans;
}
int main() {
// int t,
int cnt=1;;
while (cin >> n) {
cout << "Image number " << cnt << " contains " << solve() << " war eagles.\n";
cnt++;
}
}