zoukankan      html  css  js  c++  java
  • hdu 1241 Oil Deposits

    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
     
     
    题解:八个方向,简单搜索
     
    代码:
     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <math.h>
     4 #include <limits.h> 
     5 #include <algorithm>
     6 #include <iostream>
     7 #include <ctype.h>
     8 #include <iomanip>
     9 #include <queue>
    10 #include <map>
    11 #include <stdlib.h>
    12 using namespace std;
    13 
    14 char v[100][100];//油包分布  
    15 int m,n,i,j;
    16 
    17 void dfs(int x,int y)//当前位置(x,y)
    18 {
    19     v[x][y]='*';//将当前位置变为*  
    20     //将8个方向遍历一遍
    21     for(int dx=-1;dx<=1;dx++){
    22         for(int dy=-1;dy<=1;dy++){
    23             int px=x+dx; //移动后的位置 
    24             int py=y+dy; //移动后的位置 
    25             //判断(x,y)是否在油田内 且该点是否为油包 
    26             if(v[px][py]=='@'&&px<=n&&py<=m){ //判断是否出界 
    27                 dfs(px,py);
    28 
    29             }
    30         }
    31     }
    32 }
    33 
    34 int main()
    35 {
    36     while(~scanf("%d%d",&n,&m)){
    37         int sum=0;
    38         if(m==0&&n==0)
    39             break;
    40         for(i=0;i<n;i++)
    41             scanf("%s",v[i]);
    42         for(i=0;i<n;i++){
    43             for(j=0;j<m;j++){
    44                 if(v[i][j]=='@'){
    45                     dfs(i,j); //从有@的地方开始dfs
    46                     sum++;
    47                 }
    48             }
    49         }
    50         printf("%d
    ",sum);
    51     }
    52 }
  • 相关阅读:
    通过python连接数据库以及操作
    切片
    文件的内容读写(二)
    文件的内容读写(一)
    python数据类型(四)之嵌套类型
    python数据类型(三)之字典类型
    python数据类型(二)之数组和数组函数
    Python的内存管理
    *args 和 **kwargs 的区别
    python的命名空间
  • 原文地址:https://www.cnblogs.com/wangmengmeng/p/5035582.html
Copyright © 2011-2022 走看看