HDU:Oil Deposits

来源:互联网 发布:unity3d 骰子模型 编辑:程序博客网 时间:2024/06/06 00:30
Oil Deposits
Problem 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. 
翻译:
geosurvcomp地质调查公司负责探测地下石油储量。geosurvcomp在一个大的矩形区域的土地上工作,并创建一个网格把土地分成很多广场地块。然后分别对每一个小区分别进行分析,利用传感设备确定小区是否含有油。如果两个方格中都是油田且这两个方格是相邻的,那么他们是相同的石油存款的一部分。石油储量相当大,而且可能蕴藏大量的石油储量。你的工作是确定在一个网格中包含多少不同的石油储量。 
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.
输入文件包含一个或多个方格,
多组测试数据,m,n,代表这是一个m*n的矩阵,代表它所勘探的区域。‘*’代表这个方格区域下面没有石油,‘@’代表这个方格区域下面有石油。输入以0,0结束。
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.
输出含有多少个油田,只要有石油的方格是相邻的那么我们都认为这是同一个油田。输出所给的m*n这个矩形区域含有多少个不同的油田。
Sample Input
1 1*3 5*@*@***@***@*@*1 8@@****@*5 5 ****@*@@*@*@**@@@@*@@@**@0 0
Sample Output
0122
解题思路:搜索入门题,用dfs深搜。
#include<iostream>
#include<stdio.h>
using namespace std;
int dir[8][2]={{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}}; //根据题目意思,可以搜索八个方向
char mmap[103][103];//地图
int m,n;//规模m行n列
void search_Oil_Deposits(int x,int y)
{

        mmap[x][y]='*';//这点到过了,变为‘*’,下次就不能走这个位置了
        for(int i=0;i<8;i++)//进行八个方向的搜索
        {
            int dx=x+dir[i][0];
            int dy=y+dir[i][1];
            if(mmap[dx][dy]=='@'&&(dx>=1&&dx<=m)&&(dy>=1&&dy<=n))//如果该点是油田,且这点没有越界
            {
                search_Oil_Deposits(dx,dy);//以该点为始点再次深搜
            }
        }
}
int main()
{
    int sum,i,j;                          //m为行,n为列
    while(scanf("%d%d",&m,&n))
    {
        sum=0;
        if(m == 0 && n == 0)
            break;
        for(i = 1;i <= m; i++)
            for(j = 1;j <= n; j++)
                scanf(" %c",&mmap[i][j]);
        for(i = 1;i <= m; i++)
            for(j = 1;j <= n; j++)
            {
                if(mmap[i][j]=='@')
                {
                    sum++;
                    search_Oil_Deposits(i,j);
                }
            }
        printf("%d\n",sum);
    }
    return 0;
}
8     1     2
7     @    3
6     5     4
direction【8】【2】
       x      y
       0      1
0     -1     0        代表方位1
1     -1     1        代表方位2 
2      0      1       代表方位3
3      1      1       代表方位4
4      1      0       代表方位5
5      -1     -1     代表方位6
6      0      -1      代表方位7
7     -1      1       代表方位8
如果发现一个地方是油田,我们将这里标记变为'*',说明这个地方已经找过了,我们再在他的八个方位进行搜索,一但搜到它周边有相邻的油田,我们将此处标记为'*',再在这个油田周围进行搜索,如果某个油田周围全都搜过了没有发现任何油田,就会返回上一层,接着按顺时针方向进行搜索。

       
0 0
原创粉丝点击