hdu 1241 Oil Deposits - bfs
Description
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.
Input
The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket.
Output
For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.
Sample Input
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
Sample Output
0
1
2
2
思路:
先把所有点标记为0,所有的'.'标记为1。然后从(0,0)开始搜索,如果遇到标记为0的就把他加入队列,并把他标记为1,再看他周围8个点,如果有遇到标记为0的就把他加入队列并标记为1,重复此步骤,直到队列为空。
当一个队列处理完后再从(0,0)开始搜索,重复以上步骤,直到搜索到右下角最后一个点。
源代码:
1 #include <iostream>
2 #include<stdio.h>
3 #include<queue>
4 #include<string.h>
5 using namespace std;
6 char s[110][110];
7 int visit[110][110];
8 int main()
9 {
10 int l,c,i,j,a,b;
11 queue<int>q;
12 while(scanf("%d %d",&l,&c)) //l行,c列
13 {
14 if(l==0)break;
15 int sum=0;
16 memset(visit,0,sizeof(visit));
17 for(i=0;i<l;i++)
18 {
19 scanf("%s",s[i]);
20 for(j=0;j<c;j++) //'.'标记为1
21 {
22 if(s[i][j]=='.')
23 visit[i][j]=1;
24 }
25 }
26
27 for(a=0;a<l;a++)
28 {
29 for(b=0;b<c;b++)
30 {
31 if(s[a][b]=='@'&&visit[a][b]==0) //从(0,0)开始找到第一个标记为0的
32 {
33 q.push(a);
34 q.push(b);
35 sum++;
36 while(!q.empty())
37 {
38 i=q.front();
39 q.pop();
40 j=q.front();
41 q.pop();
42 if(j-1>=0&&i-1>=0&&s[i-1][j-1]=='@'&&visit[i-1][j-1]==0)
43 {visit[i-1][j-1]=1;
44 q.push(i-1);
45 q.push(j-1);
46 // printf("左上 %d %c\n",visit[i-1][j-1],s[i-1][j-1]);
47 }
48 if(i-1>=0&&s[i-1][j]=='@'&&visit[i-1][j]==0)
49 {visit[i-1][j]=1;
50 q.push(i-1);
51 q.push(j);
52 // printf("上 %d %c\n",visit[i-1][j],s[i-1][j]);
53 }
54 if(i-1>=0&&j+1<c&&s[i-1][j+1]=='@'&&visit[i-1][j+1]==0)
55 {visit[i-1][j+1]=1;
56 q.push(i-1);
57 q.push(j+1);
58 // printf("右上 %d %c\n",visit[i-1][j+1],s[i-1][j+1]);
59 }
60 if(j-1>=0&&s[i][j-1]=='@'&&visit[i][j-1]==0)
61 {visit[i][j-1]=1;
62 q.push(i);
63 q.push(j-1);
64 //printf("左 %d %c\n",visit[i][j-1],s[i][j-1]);
65 }
66 if(j+1<c&&s[i][j+1]=='@'&&visit[i][j+1]==0)
67 {
68 visit[i][j+1]=1;
69 q.push(i);
70 q.push(j+1);
71 //printf("右 %d %c\n",visit[i][j+1],s[i][j+1]);
72 }
73 if(i+1<l&&j-1>=0&&s[i+1][j-1]=='@'&&visit[i+1][j-1]==0)
74 {
75 visit[i+1][j-1]=1;
76 q.push(i+1);
77 q.push(j-1);
78 // printf("左下 %d %c\n",visit[i+1][j-1],s[i+1][j-1]);
79 }
80 if(i+1<l&&s[i+1][j]=='@'&&visit[i+1][j]==0)
81 {
82 visit[i+1][j]=1;
83 q.push(i+1);
84 q.push(j);
85 //printf("下 %d %c\n",visit[i+1][j],s[i+1][j]);
86 }
87 if(i+1<l&&j+1<c&&s[i+1][j+1]=='@'&&visit[i+1][j+1]==0)
88 {
89 visit[i+1][j+1]=1;
90 q.push(i+1);
91 q.push(j+1);
92 //printf("右下 %d %c\n",visit[i+1][j+1],s[i+1][j+1]);
93 }
94 }//while
95 }//if
96 }//for
97 }//for
98
99 printf("%d\n",sum);
100 }
101 return 0;
102 }

浙公网安备 33010602011771号