码迷,mamicode.com
首页 > 其他好文 > 详细

hdu1241 Oil Deposits

时间:2021-02-09 12:26:13      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:any   VID   ++   oid   rgba   acm   tin   log   dfs   

Oil Deposits

hdu1241

题目

Description

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

题目大意

GeoSurvComp地质调查公司负责探测地下石油储量。GeoSurvComp公司在一段时间内在一大块矩形区域内工作,创造出一个网格把土地分成很多方块。如果每一方块单独分析,使用感应设备去测定方块中是否含有石油。含有油的方块称为口袋。如果两个口袋是相邻的,那它们属于同一个油床。油床可以相当大,可以包含众多口袋。你的任务就是测定出有多少不同的油床。

输入:

输入文件包括一个或多个网格。每一个网格输入的第一行含有m和n,分别代表网格的横坐标和纵坐标,被一个空格分开。如果m=0则标志着输入结束;否则1<=m<=100并且1<=n<=100。接下来输入m行n列个网格特征(不记录换行符)。每一个特征值跟一个方块相对应,并且要么是*(代表没油),要么是@代表一个石油口袋。

输出:

对于每一个方块,输出不同的油田数。两个不同的口袋属于同一个油田,如果它们垂直水平或对角相邻。一个油田包含少于100个口袋。

解题思路

我们可以用遍历的方法,搜到一片油田就将这一片油田变为"*",这样就可以很轻松的A掉了。

代码

#include <iostream>

using namespace std;
char arr[101][101];
int m,n,dir[8][2]={{-1,-1},{-1,0},{-1,1},{0,1},{0,-1},{1,-1},{1,0},{1,1}};
void dfs(int i,int j)
{
    for(int q=0;q<8;q++)
    {
        int i1=i+dir[q][0];
        int j1=j+dir[q][1];
        if(i1>=1&&i1<=m&&j1>=0&&j1<n&&arr[i1][j1]==‘@‘)
        {
            arr[i1][j1]=‘*‘;
            dfs(i1,j1);
        }
    }
}
int main()
{
    int num;
    while(cin>>m>>n &&(m!=0&&n!=0))
    {
        num=0;
       for(int i=1;i<=m;i++)
            cin>>arr[i];
       for(int i=1;i<=m;i++)
            for(int j=0;j<=n;j++)
       {
           if(arr[i][j]==‘@‘)
           {
               arr[i][j]=‘*‘;
               num++;
               dfs(i,j);
           }
       }
       cout<<num<<endl;
    }
    return 0;
}

hdu1241 Oil Deposits

标签:any   VID   ++   oid   rgba   acm   tin   log   dfs   

原文地址:https://www.cnblogs.com/Jernewson/p/14391257.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!