【题目描述】
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.
GeoSurvComp地质调查公司负责地下石油矿床的探测。GeoSurvComp一次使用一个很大的矩形区域,并创建一个网格,将土地划分成许多正方形的地块。然后,它分别分析每个地块,使用传感设备来确定该地块是否含有石油。一个含有油的地块叫做口袋。如果两个小孔相邻,那么它们是同一油田的一部分。石油储量可能相当大,而且可能含有许多小孔。你的工作是确定在一个网格中包含了多少不同的石油储量。
【输入】
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.
输入文件包含一个或多个网格。每个网格以包含m和n的一行开始,这是网格中的行数和列数,由单个空格分隔。如果m=0时,则表示输入结束;否则,1 <= m <= 100 and 1 <= n <= 100。后面是n个字符的m行(不包括行尾字符)。每个字符对应于一个情节,要么是‘*’,代表没有油,要么‘@’,代表一个油袋。
【输出】
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.
对于每个网格,输出不同的油库数量。两个不同的口袋是同一油藏的一部分,如果它们是水平、垂直或对角线相邻的。一个石油矿床不会有超过100个口袋。
【样例输入】
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
【样例输出】
0
1
2
2
题目链接:https://cn.vjudge.net/problem/HDU-1241
dfs就完事了
代码如下:
#include <iostream>
#include <cstring>
using namespace std;
static const int MAXN=100;
static const int dx[]={-1,-1,-1,0,0,1,1,1},dy[]={-1,0,1,-1,1,-1,0,1};
char mp[MAXN+10][MAXN+10];
bool vis[MAXN+10][MAXN+10];
int m,n;
void dfs(int x,int y)
{
for(int i=0;i<8;i++)
{
if(1<=x+dx[i] && x+dx[i]<=m && 1<=y+dy[i] && y+dy[i]<=n && mp[x+dx[i]][y+dy[i]]=='@' && !vis[x+dx[i]][y+dy[i]])
{
vis[x+dx[i]][y+dy[i]]=true;
dfs(x+dx[i],y+dy[i]);
}
}
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0),cout.tie(0);
while(cin>>m>>n && m && n)
{
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
cin>>mp[i][j];
memset(vis,false,sizeof(vis));
int cnt=0;
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
if(mp[i][j]=='@' && !vis[i][j])
{
vis[i][j]=true;
dfs(i,j);
cnt++;
}
cout<<cnt<<endl;
}
return 0;
}