Sample Input
1 1 * 3 5 *@*@* **@** *@*@* 1 8 @@****@* 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ 0 0
Sample Output
0 1 2 2
本题比较简单 常规DFS
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <algorithm> 5 #include <memory.h> 6 7 using namespace std; 8 9 const int MAX_SIZE = 100; 10 11 char g[MAX_SIZE][MAX_SIZE]; 12 13 int n, m; 14 int oilCount = 0; 15 16 int addx[] = { -1,-1,-1,0,0,1,1,1}; 17 int addy[] = { -1,0,1,-1,1,-1,0,1}; 18 19 20 void DFS(int x,int y) 21 { 22 if (x < 0 || x >= n || y < 0 || y >= m) return; 23 if (g[x][y] != '@') return; 24 g[x][y] = '*'; 25 for (int i = 0; i < 8; i++) { 26 int newx = x + addx[i]; 27 int newy = y + addy[i]; 28 29 DFS(newx, newy); 30 } 31 } 32 33 34 int main() 35 { 36 while (1) { 37 cin >> n >> m; 38 if (n == m && m == 0) break; 39 memset(g, 0, sizeof g); 40 oilCount = 0; 41 for (int i = 0; i < n; i++) { 42 scanf("%s",g[i]); 43 } 44 45 for (int i = 0; i < n; i++) { 46 for (int j = 0; j < m; j++) { 47 if (g[i][j] == '@') { 48 DFS(i, j); 49 oilCount++; 50 } 51 } 52 } 53 54 cout << oilCount << endl; 55 } 56 57 58 return 0; 59 }View Code