Oil Deposits hdu 1214(dfs)

題目地址:點擊打開鏈接
 Oil Deposits
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
 

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

題目理解:對角線也算是一個人油田的範圍,求油田的總數目

                             

思路:1 對角線的存在可利用下面8個方向實現

int move={  {-1,-1},{-1,0},{-1,1},
                   {1,  -1},{1, 0},{1, 1},
                   {0,  -1},{0, 1}  }
                                                

      2 採用DFS進行深搜

      3 採用BFS與隊列或結構體結合

DFS

//求的是油田的個數 
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
char plot[105][105];
int M, N, ans;
void dfs(int i, int j)//深搜 
{
    if(plot[i][j]!='@' || i<1 || j<1 || i>M || j>N) return;  
    else
    {
	plot[i][j] = '!';  
        dfs(i-1, j-1); //等價於move的八個方向 
        dfs(i-1, j);  
        dfs(i-1, j+1);  
        dfs(i, j-1);  
        dfs(i, j+1);  
        dfs(i+1, j-1);  
        dfs(i+1, j);  
        dfs(i+1, j+1); 
    }
}
int main()
{
	int i, j;
	while( cin>>M>>N, M||N )
	{
		
		ans=0;	
		for(i=1; i<=M; i++)
		    for(j=1; j<=N; j++)
			    cin>>plot[i][j];
		for(i=1; i<=M; i++)
		for(j=1; j<=N; j++)
		{
			if( plot[i][j] == '@'  )
			{	
				dfs(i, j);
				ans++;
			}
		}
	    cout<<ans<<endl; 
	}
	return 0;
}


採用BFS與隊列
#include<stdio.h>  
#include<string.h>  
#include<iostream>
#define max 105  
using namespace std;
char map[max][max];  
int n,m,used[max][max];  
int dir[8][2]={  {-1, 0}, {1, 0},{0,-1},{0,1},
                 {-1,-1},{-1,1},{1,-1},{1,1}};  
struct node  
{     int x, y;    };  
struct node queue[max*max],position,temp;  
void bfs(int sx,int sy)  
{  
    int i, j, front=0, rear=0;  
    rear++;  
    queue[rear].x = sx;
    queue[rear].y = sy;  
    while( front != rear )  
    {   front++;  
        position.x = queue[front].x;
	    position.y = queue[front].y;  
        for(i=0; i<8; i++)//八個方向  
        {   
		    temp.x = position.x+dir[i][0];  
            temp.y = position.y+dir[i][1];  
            if( temp.x>=0 && temp.y>=0 && temp.x<n && temp.y<m )
		    //座標不越界 
	        if(used[temp.x][temp.y]==0 &&  map[temp.x][temp.y]=='@') 
	        //是未查詢過的點              是地圖上的'@' 
            {   rear++;  
                queue[rear].x = temp.x;
		        queue[rear].y = temp.y;  
                used[temp.x][temp.y] = 1;  
}   }   }   } 
int main()  
{  
    int i,j,num;  
    while( cin>>n>>m, n||m )  
    {  
        num=0;//記錄油田的總數  
        memset(used, 0, sizeof(used));//標記是否已經查詢過  
        for(i=0; i<n; i++)//簡化輸入   
            cin>>map[i];   
        for(i=0; i<n; i++)  
        for(j=0; j<m; j++)  
            if( map[i][j]=='@' && used[i][j]==0 )  
            {      num++;      bfs(i,j);      }  
        cout<<num<<endl;  
    }   
}  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章