程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> hdu 1241 Oil Deposits (dfs)

hdu 1241 Oil Deposits (dfs)

編輯:C++入門知識

Oil Deposits
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7813    Accepted Submission(s): 4583

 

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.

 

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

Source
Mid-Central USA 1997


Recommend
Eddy

題意:一塊油田。@代表有油,*代表沒油。數有多少塊有油的地(水平方向、豎直方向或斜線方向有@在一起的只能當做整體算一次)。

分析:直接兩重循環,碰到@,計數器+1並且dfs將該位置周圍相鄰的@全部改為*,避免計數時重復。

 

代碼:

 

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

char maps[105][105];
int dx[]={-1,1,0,0,-1,1,-1,1};
int dy[]={0,0,-1,1,-1,1,1,-1};
int r,c,flag;

void dfs(int x,int y)
{
    maps[x][y]='*';
    int xx,yy;
    for(int i=0;i<8;i++)
    {
        xx=x+dx[i];
        yy=y+dy[i];

        if(xx>=1&&xx<=r&&yy>=1&&yy<=c&&maps[xx][yy]=='@')
        {
			//printf("test: %d %d\n",xx,yy);
            maps[xx][yy]='*';
            dfs(xx,yy);
        }
    }
}

int main()
{
    int i,j,res;
	char ch[30];
    while(scanf("%d%d",&r,&c)&&r&&c)
    {
		//getchar();
		//getchar();
		gets(ch);      //也可以用兩次getchar()
        res=0;
        for(i=1;i<=r;i++)
			gets(maps[i]+1);
        for(i=1;i<=r;i++)
			for(j=1;j<=c;j++)
            {
                if(maps[i][j]=='@')
                {
                    ++res;
                    dfs(i,j);
                }
            }
        printf("%d\n",res);
    }
    return 0;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved