HDU 1241 Oil Deposits
1 # include <iostream>
2 # include <queue>
3 # include <cstdio>
4 using namespace std;
5 int n, m;
6 int a[110][110];
7 int step;
8 struct node
9 {
10 int x;
11 int y;
12 }pp[110][110];
13 char arr[110][110];
14 int dir[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1} };
15 int BFS()
16 {
17 int num = 0, i, j;
18 memset(a, 0, sizeof(a));
19 queue<node> Q;
20 node p, q;
21 for (i=0; i<n; ++i)
22 {
23 for (j=0; j<m; ++j)
24 {
25 if (a[i][j] == 0 && arr[i][j] == '@')
26 {
27 num++;
28 Q.push(pp[i][j]);
29 while (!Q.empty())
30 {
31 p = Q.front();
32 Q.pop();
33 for (int k=0; k<8; ++k)
34 {
35 q.x = p.x + dir[k][0];
36 q.y = p.y + dir[k][1];
37 if (q.x >= 0 && q.x < n && q.y >= 0 && q.y < m && arr[q.x][q.y] == '@' && a[q.x][q.y] == 0)
38 {
39 Q.push(pp[q.x][q.y]);
40 a[q.x][q.y] = 1;
41 }
42 }
43 }
44 }
45 }
46 }
47 return num;
48 }
49 int main()
50 {
51 int num;
52 while (scanf("%d %d", &n, &m) && (m + n) != 0)
53 {
54 step = 0;
55 for (int i=0; i<n; ++i)
56 {
57 for (int j=0; j<m; ++j)
58 {
59 scanf(" %c", &arr[i][j]);
60 pp[i][j].x = i;
61 pp[i][j].y = j;
62 }
63 }
64 num = BFS();
65 printf("%d\n", num);
66 }
67 return 0;
68 }
//用for遍历过去,如果这个地方是油点,而且之前没有被发现过( == 1)那么证明它是一个新的油点,我们便可以+1了,对每个新发现的油田的8个方向都进行广搜,搜到周围的油田的时候把它入队,并把它标记成已经发现了!!!!