zoukankan      html  css  js  c++  java
  • hdoj1241 Oil Deposits

    Oil Deposits

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


    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
    讲解:@代表油田,寻找共有多少个油田,连在一起的算一个
    解法一:不用队列,代码如下:
     1 #include<iostream>
     2 #include<string>
     3 #include<cstring>
     4 using namespace std;
     5 char s[110][110];
     6 int dir[8][2]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
     7 int n,m;
     8 void dfs(int x,int y)
     9 { int i,xx,yy;
    10     for(i=0;i<8;i++)
    11     {
    12         xx=x+dir[i][0];yy=y+dir[i][1];
    13      if(xx<1||xx>n || yy<1 || yy>m  || s[xx][yy]=='*')
    14                     continue;
    15         s[xx][yy]='*';
    16          dfs(xx,yy);
    17     }
    18 }
    19 int main()
    20 {
    21     int i,j;
    22     while(cin>>n>>m && m+n)
    23     {  int sum=0;
    24     for(i=1;i<=n;i++)
    25        for(j=1;j<=m;j++)
    26            cin>>s[i][j];
    27        for(i=1;i<=n;i++)
    28           for(j=1;j<=m;j++)
    29           if(s[i][j]=='@')
    30           {
    31               sum++;
    32              // s[i][j]='*';
    33               dfs(i,j);
    34           }
    35           cout<<sum<<endl;
    36     }
    37     return 0;
    38 }

    解法二:用队列来解

     1 #include<iostream>
     2 #include<queue>
     3 const int MAX=101;
     4 int fangxiang[8][2]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
     5 using namespace std;
     6 typedef struct dian
     7 {
     8     int x;
     9     int y;
    10 };
    11 char map[MAX][MAX];
    12 int n;
    13 int m;
    14 void BFS(int x,int y)
    15 {
    16     int i,j;
    17     queue<dian>que;
    18     dian in,out;
    19     in.x=x;
    20     in.y=y;
    21     que.push(in);
    22     while(!que.empty())
    23     {
    24         in=que.front();
    25         que.pop();
    26         dian next;
    27         for(i=0;i<8;i++)
    28         {
    29             next.x=out.x=in.x+fangxiang[i][0];
    30             next.y=out.y=in.y+fangxiang[i][1];
    31             if(next.x<1||next.x>n||next.y<1||next.y>m)
    32             continue;
    33             if(map[next.x][next.y]=='@')
    34             {
    35              map[next.x][next.y]='*';
    36              BFS(next.x,next.y);
    37             }
    38         }
    39     }
    40 }
    41 int main()
    42 {
    43     int i,j,sum;
    44     while(cin>>n>>m,m)
    45     {
    46         sum=0;
    47         for(i=1;i<=n;i++)
    48         {
    49             for(j=1;j<=m;j++)
    50             {
    51                 cin>>map[i][j];
    52             }
    53         }
    54         for(i=1;i<=n;i++)
    55         {
    56             for(j=1;j<=m;j++)
    57             {
    58                 if(map[i][j]=='@')
    59                 {
    60                     sum+=1;
    61                     map[i][j]='*';
    62                     BFS(i,j);
    63                 }
    64             }
    65         }
    66         cout<<sum<<endl;
    67     }
    68     return 0;
    69 }
  • 相关阅读:
    Kubernetes(十一) 部署doshboard
    kubernetes(一)kubeadm安装
    kubernetes安装-二进制
    使用Jmeter+Maven+Jenkins实现接口自动化测试
    使用Jmeter在linux环境实现分布式负载
    Jmeter连接Mysql和Oracle数据库
    Jmeter如何实现参数化用户,并且管理Cookie
    开启MYSQL慢查询日志,监控有效率问题的SQL
    使用jmeter+ant+jenkins实现接口自动化测试
    使用Jmeter对SHA1加密接口进行性能测试
  • 原文地址:https://www.cnblogs.com/lovychen/p/3216306.html
Copyright © 2011-2022 走看看