• hdu1241 Oil Deposits


                                                     Oil Deposits

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


    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
     
    DFS类型题
     1 #include<iostream>
     2 #include<cstdio>
     3 #define N 110
     4 using namespace std;
     5 
     6 int m,n;
     7 
     8 int dir[8][2]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
     9 
    10 char maps[N][N];
    11 
    12 int nx,ny;
    13 
    14 void dfs(int x,int y)
    15 {
    16 
    17     if(x<0||x>=m||y<0||y>=n)//判断当前所在位置是否合法;
    18         return ;
    19     if(maps[x][y]=='*')//递归结束条件;
    20         return ;
    21     else
    22     {
    23         maps[x][y]='*';
    24         for(int i=0;i<8;i++)//搜索相邻的8个方向;
    25         {
    26             nx=x+dir[i][0];
    27             ny=y+dir[i][1];
    28             dfs(nx,ny);//进行深搜;
    29         }
    30 
    31     }
    32 }
    33 
    34 int main()
    35 {
    36     int i,j,ans;
    37 
    38     while(cin>>m>>n,m+n)
    39     {
    40         ans=0;
    41 
    42         for(i=0; i<m; i++)
    43             scanf("%s",maps[i]);
    44         for(i=0; i<m; i++)
    45         {
    46             for(j=0; j<n; j++)
    47             {
    48                 if(maps[i][j]=='@')
    49                 {
    50                     dfs(i,j);
    51                     ans++;
    52                 }
    53             }
    54         }
    55         printf("%d
    ",ans);
    56     }
    57     return 0;
    58 }
  • 相关阅读:
    持久层框架:MyBatis 3.2(2)
    持久层框架:MyBatis 3.2(1)
    循环结构(二)
    Android LayoutInflater详解
    为什么调用 FragmentPagerAdapter.notifyDataSetChanged() 并不能更新其 Fragment?
    Android Support v4、v7、v13的区别和应用场景
    Android的string-array数据源简单使用
    FragmentTabHost切换Fragment时避免重复加载UI
    Ubuntu 安装Chrome步骤
    慢慢来,让好习惯自然来
  • 原文地址:https://www.cnblogs.com/zhengguiping--9876/p/4344198.html
Copyright © 2020-2023  润新知