UVA572 Oil Deposits

题目描述

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值