题目描述
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.
题目意思很简单,就是说图像中@符号不管是横这竖着斜着,只要连着,就是一片油田,问一共有多少油田。
日常安利本题我的博客
思路分析
虐简简单题的时候来了,本博客纯属为了凑篇数的虐菜题。这个题就是简单的DFS求连通块的问题,核心的思想就是用vis数组记录是否访问过。
代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<string>
#include <set>
#include<time.h>
using namespace std ;
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
const double eps = 1e-8;
const int maxn = 110;//须填写
const int inf = 0x3f3f3f3f;
char pic[maxn][maxn];
int vis[maxn][maxn];
int n,m;
void dfs(int x, int y, int s)
{
if(x<0||y<0||x>=m||y>=n)
return ;
for(int i=-1;i<2;i++)
{
for(int j=-1;j<2;j++)
{
if(vis[i+x][j+y] == 0&&pic[i+x][j+y]=='@')
{
vis[i+x][j+y] = s;
dfs(i+x,j+y,s);
}
}
}
}
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("sample.out", "w", stdout);
int results;
while(scanf("%d%d",&m,&n)&&n&&m)
{
results = 0;
mem(vis);
for(int i=0;i<m;i++)
{
scanf("%s",pic[i]);
}
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(vis[i][j] == 0&&pic[i][j]=='@')
{
vis[i][j] = ++results;
dfs(i,j,results);
}
}
}
printf("%d\n",results);
}
return 0;
}