-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathProblem C.cpp
More file actions
48 lines (44 loc) · 1.28 KB
/
Problem C.cpp
File metadata and controls
48 lines (44 loc) · 1.28 KB
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
#include <iostream>
using namespace std;
char image[25][25]; // matrix for image
void dfs(int dim, int i, int j) // dfs traversal
{
if(i < 0 || j < 0 || i >= dim || j >= dim || image[i][j] == '0') return; // if out of bounds or not an eagle, return
image[i][j] = '0'; // if eagle part is visited already mark as zero -> same thing as visited array
for(int k = -1; k < 2; k++)
{
for(int l = -1; l < 2; l++) // for valid indexes
{
if (k || l) dfs(dim, i+k, j+l); // if there is an eagle part, run dfs on it
}
}
}
int eagleCount(int dim) // run dfs on all eagle parts
{
int count = 0;
for (int i=0; i<dim; i++)
{
for (int j=0; j<dim; j++)
{
if (image[i][j] == '1') // if eagle part is found
{
dfs(dim, i, j); // dfs will traverse all eagle parts that are joint to this one; thus just one count
count++;
}
}
}
return count;
}
int main()
{
int dim, images = 0;
while (cin>>dim)
{
images++; // number of images
for (int i=0; i<dim; i++) // input for images
{
cin>>image[i];
}
cout<<"Image number "<<images<<" contains "<<eagleCount(dim)<<" war eagles."<<endl;
}
}