zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    Navicat 安装教程
    office 2016 安装教程
    PyCharm 2018.1.1软件安装教程
    Pytorch学习笔记02----深度学习中的epochs,batch_size,iterations详解
    Anaconda 安装步骤
    Python基础汇总001_txt文件读写、字典使用等
    Pytorch学习笔记01----pytorch框架介绍
    es6的学习
    vue的使用与安装 npm -v报错
    js弹窗返回值详解(window.open方式)
  • 原文地址:https://www.cnblogs.com/zhengguiping--9876/p/4344198.html
Copyright © 2011-2022 走看看