HDU-1241 Oil Deposits(DFS)(BFS)

版权声明:商业转载请联系作者获得授权,非商业转载请注明出处。 https://blog.csdn.net/weixin_43939327/article/details/85085850

Oil Deposits

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 46073 Accepted Submission(s): 26549

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

题目简述:

(看看就行qaq)有家公司挖石油的。每块油田被划分成很多个小矩形,输出会逐行表示出油田的情况。用‘@’表示油矿,连在一起的油矿算为同一个一油矿。用’#'表示没有油矿。问油田中有几个油矿。
每组输出第一行是油田行数m和列数n。接下来m行每行给出n个字符(‘#’或‘@’)代表每块格子有无油矿。

题目分析:

用DFS搜图就好。当然BFS也行(不了解BFS或DFS的可以看看这篇文章 https://blog.csdn.net/jinzhao1993/article/details/51166850 )

代码实现(DFS):

#include<bits/stdc++.h>
using namespace std;

int n,m,tx,ty;
char c[105][105];
int derection[8][2]={1,0,1,1,0,1,-1,1,-1,0,-1,-1,0,-1,1,-1};

void dfs(int x,int y)
{
    c[x][y]='*';
    for(int i=0;i<8;i++)
    {
        tx=x+derection[i][0];
        ty=y+derection[i][1];
        if(c[tx][ty]=='@')
            dfs(tx,ty);
    }
}

int main()
{
    int i,j,sum;
	while(scanf("%d%d",&n,&m)&&n&&m)
	{
	    memset(c,'*',sizeof(c));
        for(i=0;i<n;i++)
            scanf("%s",c[i]);
	    sum=0;
	    for(i=0;i<n;i++)
	        for(j=0;j<m;j++)
                if(c[i][j]=='@')
                {
                    sum++;
                    dfs(i,j);
                }
	    printf("%d\n",sum);
	}
}

代码实现(BFS):

#include<bits/stdc++.h>
using namespace std;

int n,m;
char c[105][105];
int derection[8][2]={1,0,1,1,0,1,-1,1,-1,0,-1,-1,0,-1,1,-1};

struct point
{
	int x;
	int y;
}temp;

void bfs(int x,int y)
{
    int i;
	queue<point> line;
	point p1;
	p1.x=x;
	p1.y=y;
	line.push(p1);
	while(!line.empty())
	{
		p1=line.front();
		line.pop();
		for(i=0;i<8;i++)
        {
            temp.x=p1.x+derection[i][0];
            temp.y=p1.y+derection[i][1];
            if(temp.x<n&&temp.y<m&&temp.x>=0&&temp.y>=0&&c[temp.x][temp.y]=='@')
            {
                c[temp.x][temp.y]='*';
                line.push(temp);
            }
        }
	}
}

int main()
{
    int i,j,sum;
	while(scanf("%d%d",&n,&m)&&n&&m)
	{
	    memset(c,'*',sizeof(c));
        for(i=0;i<n;i++)
            scanf("%s",c[i]);
	    sum=0;
	    for(i=0;i<n;i++)
	        for(j=0;j<m;j++)
                if(c[i][j]=='@')
                {
                    sum++;
                    bfs(i,j);
                }
	    printf("%d\n",sum);
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_43939327/article/details/85085850