• 杭电 1241 Oil Deposits (很好的dfs)


    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
    定义个数组,让@的出现的位置为1,*出现的位置为0;从@出现的位置开始找,找到一个1,把它变成0,具体看测试结果。
     1 #include<cstdio>
     2 #include<string.h>
     3 int i,j,m,n,map[110][110];
     4 char cha[110],ans;
     5 int dx[8]={-1,0,1,1,1,0,-1,-1};
     6 int dy[8]={-1,-1,-1,0,1,1,1,0};
     7 void f(int x,int y)
     8 {
     9     int i,nx,ny;
    10     for(i = 0 ; i < 8 ; i++)
    11     {
    12         nx=x+dx[i];
    13         ny=y+dy[i];
    14         if(nx>=0 && ny>=0 && nx<m && ny<n && map[nx][ny]==1)
    15         {
    16             map[nx][ny]=0;
    17             f(nx,ny);
    18          } 
    19     }
    20 }
    21 int main()
    22 {
    23     while(scanf("%d %d",&m,&n)&&m&&n)
    24     {
    25         memset(map,0,sizeof(map)); 
    26         ans=0;
    27         for(i = 0 ; i < m ; i++)
    28         {
    29             scanf("%s",&cha);
    30             for(j = 0 ; j < n ; j++)
    31             {
    32                 if(cha[j] == '@')
    33                 {
    34                     map[i][j]=1;
    35                 }        
    36             }        
    37         }    
    38     /*    for( i = 0 ; i < m ; i++)
    39         {
    40             for(j =0 ; j < n ; j++)
    41             {
    42                 printf("%d  ",map[i][j]);
    43                 if(j == n-1)
    44                 {
    45                     printf("
    ");
    46                 }
    47             }
    48         }*/
    49         for(i = 0 ; i < m ; i++)
    50         {
    51             for(j = 0 ; j < n ; j++)
    52             {
    53                 if(map[i][j] == 1)
    54                 {
    55                     map[i][j]==0;
    56                     f(i,j);
    57                     ans++;            
    58                 }
    59             }
    60         }
    61         printf("%d
    ",ans);
    62     }
    63 }

    bfs解法

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<queue>
     4 using namespace std;
     5 int m,n,map[110][110],ans,i,j;
     6 char str[110];
     7 int dx[8]={-1,0,1,1,1,0,-1,-1};
     8 int dy[8]={-1,-1,-1,0,1,1,1,0};
     9 struct stu
    10 {
    11     int x,y;
    12 }st;
    13 void bfs(int xx,int yy)
    14 {
    15     stu next;
    16     int i;
    17     st.x=xx;
    18     st.y=yy;
    19     queue<stu>que;
    20     que.push(st);
    21     while(!que.empty())
    22     {
    23         st=que.front();
    24         que.pop();
    25         for(i = 0 ; i < 8 ; i++)
    26         {
    27             int nx,ny;
    28             nx=st.x+dx[i];
    29             ny=st.y+dy[i];
    30             if(nx>=0 && ny>=0 && nx<m && ny<n && map[nx][ny]==1)
    31             {
    32                 map[nx][ny]=0;
    33                 next.x=nx;
    34                 next.y=ny;
    35                 que.push(next);
    36             }
    37         }
    38         
    39     }
    40 }
    41 int main()
    42 {
    43     while(scanf("%d %d",&m,&n) && m && n)
    44     {
    45         memset(map,0,sizeof(map));
    46         ans=0;
    47         for(i = 0 ; i < m ; i++)
    48         {
    49             scanf("%s",&str);
    50             for(j = 0 ; j < n ; j++)
    51             {
    52                 if(str[j] == '@')
    53                 {
    54                     map[i][j]=1;
    55                 }
    56             }
    57         }    
    58         for(i = 0 ; i < m ;i++)
    59         {
    60             for(j = 0 ;j < n ; j++)
    61             {
    62                 if(map[i][j] == 1)
    63                 {
    64                     map[i][j]=0;
    65                     bfs(i,j);
    66                     ans++;
    67                 }
    68             }
    69         }
    70         printf("%d
    ",ans);
    71     }
    72 }
    ——将来的你会感谢现在努力的自己。
  • 相关阅读:
    IDEA 的默认tomcat日志位置以及默认编译后的classes位置
    在linux环境下tomcat 指定 jdk或jre版本
    在Linux环境如何在不解压情况下搜索多个zip包中匹配的字符串内容
    说一下最近找的工作所遇到的一个巨坑,跟各位同行分享一下。(与技术无关)
    Jmeter 深入性能测试进阶-01
    英语
    python 01
    fiddler,ADB, Monkey
    http 协议,SSL证书,http头信息,tcp/http区别,支付功能测试
    扎马步-计算机网络和系统基础知识
  • 原文地址:https://www.cnblogs.com/yexiaozi/p/5716243.html
Copyright © 2020-2023  润新知