Find the first in-depth Priority Search question and find the number of eight connected blocks in the figure.
# Include <iostream>
# Include <cstring>
Using namespace STD;
# Define x 100
Int visit [x] [x];
Int A [x] [x];
Void DFS (int x, int y) // deep Priority Search
{
If (visit [x] [Y] |! A [x] [Y]) // if it is white or traversed
Return;
Visit [x] [Y] = 1; // mark the traversal
DFS (x-1, Y-1); DFS (x-1, Y); DFS (x-1, Y + 1); // recursively traverse the surroundings
DFS (x, Y-1); DFS (X, Y + 1 );
DFS (x + 1, Y-1); DFS (x + 1, Y); DFS (x + 1, Y + 1 );
}
Int main ()
{
Freopen ("sum. In", "r", stdin );
Freopen ("sum. Out", "W", stdout );
Int N, I, j;
While (CIN> N)
{
Memset (A, 0, sizeof ());
For (I = 1; I <= N; I ++)
For (j = 1; j <= N; j ++)
Cin> A [I] [J];
Int COUNT = 0;
Memset (visit, 0, sizeof (visit); // expand the boundary to make it white.
For (I = 1; I <= N; I ++)
For (j = 1; j <= N; j ++)
If (! Visit [I] [J] & A [I] [J])
{
Count ++; // count
DFS (I, j); // deep Priority Search
}
Cout <count <Endl;
}
Return 0;
}