The question means:
To make sure that there are several eagles in the picture, each pixel if it is ' 1 ' represented as a eagles, but the ' 1 ' that is connected to the top and bottom (with a total of 8 angles) can only be considered the same.
Idea:
Using DFS to find ' 1 ' has several regions.
1#include <cstdio>2 using namespacestd;3 4 Charimage[ -][ -];5 6 voidDFS (int&n,intIintj)7 {8IMAGE[I][J] ='0';9 if(I-1>=0&& image[i-1][J] = ='1') DFS (n, I-1, j);Ten if(i+1< n && image[i+1][J] = ='1') DFS (N, i+1, j); One if(J-1>=0&& image[i][j-1] =='1') DFS (n, I, J1); A if(j+1< n && image[i][j+1] =='1') DFS (n, I, j+1); - if(I-1>=0&& J1>=0&& image[i-1][j-1] =='1') DFS (n, I-1, J-1); - if(I-1>=0&& j+1< n && image[i-1][j+1] =='1') DFS (n, I-1, j+1); the if(i+1< n && J1>=0&& image[i+1][j-1] =='1') DFS (N, i+1, J-1); - if(i+1< n && j+1< n && image[i+1][j+1] =='1') DFS (N, i+1, j+1); - } - + intMain () - { + //freopen ("Input.txt", "RT", stdin); A intN,case =1; at while(SCANF ("%d", &n)! =EOF) { - GetChar (); - for(inti =0; I < n; ++i) - gets (Image[i]); - intnum =0; - for(inti =0; I < n; ++i) in for(intj =0; J < N; ++j) - if(Image[i][j] = ='1'){ to DFS (n, I, j); +++num; - } theprintf"Image number%d contains%d war eagles.\n", case++, num); * } $ return 0;Panax Notoginseng}
UVA 352-the Seasonal War