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
#include
#include
#include
#include
#include
#include 
#include
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

你可能感兴趣的:(UVA572,poj,搜索)