POJ-1562 Oil Deposits (oil field-dfs)

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

Title:

It is to give you a map, where'@' represents a place where oil is detected, and'*' represents a place where there is no oil. Now the title gives the definition of an oil field. All the'@'s connected together represent an oil field, and the definition of a connection together has a common point or a common edge. The requirement of the subject is to output the number of oil fields on the map.

Ideas:

Because the definition of linking together in the title has a common point or common edge. So we must first find an'@' and then look for @ near it, so we can set a direction array to control the direction of the search, and mark it to prevent an oil field from being repeatedly searched for errors. (The overall idea is to find an @ first, then search in a counterclockwise direction to find the mark and search for it in a counterclockwise direction).

AC code

#include<stdio.h>
char a[101][101];
int m,n;
int xy[8][2] = {
    
    {
    
    1,0},{
    
    1,1},{
    
    0,1},{
    
    -1,1},{
    
    -1,0},{
    
    -1,-1},{
    
    0,-1},{
    
    1,-1}};//逆时针方向进行遍历 
void dfs(int x,int y); 
 
int main()
{
    
    
	int i,j,t;
    while(scanf("%d%d",&m,&n)&&(m!=0&&n!=0))
    {
    
    
        t=0;
        for(i=0;i<m;i++)
       {
    
    
	   		scanf(" %s",a[i]);
		}
        for(i=0;i<m;i++)
        {
    
    
        	for(j=0;j<n;j++)
            {
    
    
                if((a[i][j]=='@'))
                {
    
    
                	dfs(i,j);
                	t++;
				}
            }
		}
        printf("%d\n",t);
    }
    return 0;
}
void dfs(int x,int y)
{
    
    
	int i;
    if((x>=0||x<m||y>=0||y<n)&&(a[x][y]=='@'))//查看是否超出边界 
    {
    
    
    	a[x][y]='#';//标记,防止寻找相邻的时重复遍历消耗时间和出现错误  
    	for(i=0;i<=7;i++)//按照逆时针方向进行遍历寻找是否有相邻的'@' 
    	{
    
    
			 dfs(x+xy[i][0],y+xy[i][1]); 
   		 } 
    } 
}

Guess you like

Origin blog.csdn.net/weixin_46703995/article/details/108877461