一次AC,虽然是水题,虽然算法写得不是很好,但是一次AC还是很爽的。。
//#define LOCAL #include <stdio.h> #include <string.h> #define MAXN 100 + 10 char graph[MAXN][MAXN]; int count; int m, n; void DFS(int p, int q); int main() { #ifdef LOCAL freopen("C:\\Users\\Administrator\\Desktop\\Temp\\ACMTempIn.txt", "r", stdin); //freopen("C:\\Users\\Administrator\\Desktop\\Temp\\ACMTempOut.txt", "w", stdout); #endif int i, j; while(scanf("%d%d", &m, &n) && m != 0) { // 初始化 count = 0; memset(graph, 0, sizeof(graph)); // 数据输入 for(i = 0; i < m; i++) { for(j = 0; j < n; j++) { scanf("\n%c", &graph[i][j]); } } // 算法主体 for(i = 0; i < m; i++) for(j = 0; j < n; j++) if(graph[i][j] == '@') { DFS(i, j); count++; } // 数据输出 printf("%d\n", count); } return 0; } void DFS(int p, int q) { if(p >= 0 && p <= m - 1 && q >= 0 && q <= n - 1 && graph[p][q] == '@') { graph[p][q] = '*'; } else { return; } DFS(p - 1, q - 1); DFS(p - 1, q); DFS(p - 1, q + 1); DFS(p, q - 1); DFS(p, q + 1); DFS(p + 1, q - 1); DFS(p + 1, q); DFS(p + 1, q + 1); }