题目大意:和UVa 572 - Oil Deposits一下,寻找“八连块”的个数。用DFS解决。
1 #include <cstdio> 2 #include <cstring> 3 #define MAXN 30 4 5 int n; 6 int G[MAXN][MAXN]; 7 bool vis[MAXN][MAXN]; 8 9 void dfs(int i, int j) 10 { 11 if (i < 0 || i >= n || j < 0 || j >= n || vis[i][j] || !G[i][j]) return; 12 vis[i][j] = 1; 13 dfs(i-1, j-1); dfs(i-1, j); dfs(i-1, j+1); 14 dfs(i, j-1); dfs(i, j+1); 15 dfs(i+1, j-1); dfs(i+1, j); dfs(i+1, j+1); 16 } 17 18 int main() 19 { 20 #ifdef LOCAL 21 freopen("in", "r", stdin); 22 #endif 23 int kase = 0; 24 char str[50]; 25 while (scanf("%d", &n) != EOF) 26 { 27 for (int i = 0; i < n; i++) 28 { 29 scanf("%s", str); 30 for (int j = 0; j < n; j++) 31 G[i][j] = str[j] - '0'; 32 } 33 memset(vis, 0, sizeof(vis)); 34 int cnt = 0; 35 for (int i = 0; i < n; i++) 36 for (int j = 0; j < n; j++) 37 { 38 if (!vis[i][j] && G[i][j]) 39 { 40 dfs(i, j); 41 cnt++; 42 } 43 } 44 printf("Image number %d contains %d war eagles. ", ++kase, cnt); 45 } 46 return 0; 47 }