ZOJ-1709

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

典型的BFS问题;

AC代码为:

复制代码
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<string>
 5 #include<queue>
 6 #include<algorithm>
 7 using namespace std;
 8 int vis[110][110];
 9 int m, n;
10 char str[110][110];
11 int bfx[8] = { 1,-1,0,0,1,1,-1,-1 };
12 int bfy[8] = { 0,0,1,-1,1,-1,1,-1 };
13 queue<int> q;
14 void  BFS(int i, int j)
15 {
16 while (!q.empty())
17 q.pop();
18 q.push(i*n + j);
19 
20 
21 while (!q.empty())
22 {
23 int u = q.front();
24 q.pop();
25 int cx = u / n;
26 int cy = u % n;
27 
28 
29 for (int k = 0; k<8; k++)
30 {
31 int nx = cx + bfx[k];
32 int ny = cy + bfy[k];
33 
34 
35 if (nx >= 0 && nx<m && ny >= 0 && ny<n && !vis[nx][ny] && str[nx][ny] == '@')
36 {
37 vis[nx][ny] = 1;
38 q.push(nx*n + ny);
39 }
40 }
41 }
42 }
43 
44 
45 int main()
46 {
47 
48 
49 while (~scanf("%d%d", &m, &n), m || n)
50 {
51 
52 
53 memset(vis, 0, sizeof(vis));
54 int sum = 0;
55 for (int i = 0; i<m; i++)
56 {
57 scanf("%s", str[i]);
58 }
59 for (int i = 0; i<m; i++)
60 {
61 for (int j = 0; j<n; j++)
62 {
63 if (str[i][j] == '@' && !vis[i][j])
64 {
65 vis[i][j] = 1;
66 BFS(i, j);
67 sum++;
68 }
69 }
70 }
71 
72 
73 printf("%d\n", sum);
74 }
75 
76 
77 }
View Code
复制代码

 

posted @   StarHai  阅读(210)  评论(0编辑  收藏  举报
编辑推荐:
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
阅读排行:
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Ai满嘴顺口溜,想考研?浪费我几个小时
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法
点击右上角即可分享
微信分享提示