• HDU 1241.Oil Deposits-求连通块DFS or BFS


    Oil Deposits

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


    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
     
     
     
     
    代码(DFS):
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 const int N=100+10;
     4 char a[N][N];
     5 int dis[8][2]={{1,0},{-1,0},{0,1},{0,-1},{1,1},{-1,1},{1,-1},{-1,-1}};
     6 int n,m,num;
     7 void DFS(int x,int y){
     8     int xx,yy;
     9     for(int i=0;i<8;i++){
    10         xx=dis[i][0]+x;
    11         yy=dis[i][1]+y;
    12         if(xx>=0&&yy>=0&&xx<n&&yy<m){
    13             if(a[xx][yy]=='@'){
    14                 a[xx][yy]='*';
    15                 DFS(xx,yy);
    16             }
    17         }
    18     }
    19 }
    20 int main(){
    21     while(~scanf("%d%d",&n,&m)){
    22         memset(a,0,sizeof(a));
    23         if(n==0&&m==0)break;
    24         num=0;
    25         for(int i=0;i<n;i++){
    26             for(int j=0;j<m;j++)
    27                 cin>>a[i][j];
    28         }
    29         for(int i=0;i<n;i++){
    30             for(int j=0;j<m;j++){
    31                 if(a[i][j]=='@'){
    32                  num++;
    33                  a[i][j]='*';
    34                  DFS(i,j);
    35                 }
    36             }
    37         }
    38         printf("%d
    ",num);
    39     }
    40     return 0;
    41 }

    代码(BFS):

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<queue>
     5 using namespace std;
     6 const int N=100+10;
     7 char mat[N][N];
     8 int dir[8][2]={{1,0},{-1,0},{0,1},{0,-1},{1,1},{-1,1},{1,-1},{-1,-1}};
     9 int n,m,sum;
    10 struct Node{
    11     int x,y;
    12 };
    13 void BFS(int x,int y){
    14     queue<Node>q;
    15     Node node;
    16     node.x=x;
    17     node.y=y;
    18     q.push(node);
    19     while(!q.empty()){
    20         Node cur=q.front();
    21         Node next;
    22         q.pop();
    23         for(int i=0;i<8;i++){
    24             next.x=cur.x+dir[i][0];
    25             next.y=cur.y+dir[i][1];
    26             if(mat[next.x][next.y]=='@'){
    27                 mat[next.x][next.y]='*';
    28                 q.push(next);
    29             }
    30         }
    31     }
    32 }
    33 int main(){
    34     while(scanf("%d%d",&n,&m)){
    35         if(n==0&m==0)break;
    36         memset(mat,0,sizeof(mat));
    37         sum=0;
    38         int cur=1;
    39         for(int i=1;i<=n;i++)
    40             scanf("%s",mat[i]+1);
    41         for(int i=1;i<=n;i++){
    42             for(int j=1;j<=m;j++){
    43                 if(mat[i][j]=='@'){
    44                     sum++;
    45                     mat[i][j]='*';
    46                     BFS(i,j);
    47                 }
    48             }
    49         }
    50         printf("%d
    ",sum);
    51     }
    52     return 0;
    53 }
  • 相关阅读:
    类目(分类)
    协议(Protocol)---实例
    OC 复合 组装电脑
    iOS--九宫格奥秘(UIView)(arc4random)
    字符串
    oc 字符串
    七星彩问题
    OC--第一个程序
    关于行内元素垂直居中的一个小小trick
    关于orgChart
  • 原文地址:https://www.cnblogs.com/ZERO-/p/9740885.html
Copyright © 2020-2023  润新知