zoukankan      html  css  js  c++  java
  • Oil Deposits

    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.bfs
     1 #include <iostream>
     2 #include <queue>
     3 #include <cstring>
     4 using namespace std;
     5 char s[105][105];
     6 int m, n;
     7 
     8 int dir[8][2] = {
     9     {0,1},
    10     {0,-1},
    11     {1,-1},
    12     {1,0},
    13     {1,1},
    14     {-1,-1},
    15     {-1,0},
    16     {-1,1},
    17 };
    18 
    19 struct node{
    20     int x, y;
    21 }pos,q;
    22 
    23 queue<node> que;
    24 int vis[105][105];
    25 int bfs(int i, int j){
    26     memset(vis,0,sizeof(vis));
    27     pos.x = i, pos.y = j;
    28     vis[i][j] = 1;
    29     que.push(pos);
    30     while(!que.empty()){
    31         pos = que.front();
    32         que.pop();
    33         s[pos.x][pos.y] = '*';
    34         for(i = 0; i < 8; i++){
    35             int near_x = pos.x + dir[i][0];
    36             int near_y = pos.y + dir[i][1];
    37             if(near_x >= 0 && near_x < m && near_y >= 0 && near_y < n && s[near_x][near_y] == '@' && vis[near_x][near_y] == 0){
    38                 q.x = near_x;
    39                 q.y = near_y;
    40                 vis[near_x][near_y] = 1;
    41                 que.push(q);
    42             }
    43         }
    44     }
    45     while(!que.empty()){
    46         que.pop();
    47     }
    48 }
    49 int main(){
    50     while(cin >> m >> n){
    51         if(m == 0 && n == 0)
    52             break;
    53         int ans = 0;
    54         for(int i = 0; i < m; i++){
    55             cin >> s[i];
    56         }
    57         for(int i = 0; i < m; i++){
    58             for(int j = 0; j < n; j++){
    59                 if(s[i][j] == '@'){
    60                     ans++;
    61                     bfs(i, j);
    62                 }
    63             }
    64         }
    65         cout << ans << endl;
    66     }
    67 }

     2.dfs

     1 #include <iostream>
     2 #include <queue>
     3 using namespace std;
     4 int n, m;
     5 char map[105][105];
     6 int dir[8][2] = {0,1, 1,0, -1,0, 0,-1, 1,1, 1,-1, -1,1, -1,-1};
     7 void dfs(int x, int y){
     8     for(int i = 0; i < 8; i++){
     9         int xx = x + dir[i][0];
    10         int yy = y + dir[i][1];
    11         if(xx >= 1 && xx <= n && yy >= 1 && yy <= m && map[xx][yy] == '@'){
    12             map[xx][yy] = '*';
    13             dfs(xx,yy);
    14         }
    15     }
    16 }
    17 int main(){
    18     while(cin >> n >> m){
    19         if(n == 0 && m == 0)
    20             break;
    21         for(int i = 1; i <= n; i++){
    22             for(int j = 1; j <= m; j++){
    23                 cin >> map[i][j];
    24             }
    25         }
    26         int ans = 0;
    27         for(int i = 1; i <= n; i++){
    28             for(int j = 1; j <= m; j++){
    29                 if(map[i][j] == '@'){
    30                     map[i][j] = '*';
    31                     ans++;
    32                     dfs(i,j);
    33                 }
    34             }
    35         }
    36         cout << ans << endl;
    37     }
    38 }
  • 相关阅读:
    [转载] IE8+兼容小结
    vue添加,删除内容
    vue跳转的两种方法
    checked 完整版全选,单选,反选
    网页特殊符号HTML代码大全
    利用css 实现 视觉差效果
    css 自定义滚动条样式
    js 模拟键盘
    css 动画 transition和animation
    浅谈浏览器垃圾回收机制
  • 原文地址:https://www.cnblogs.com/jxust-jiege666/p/6684052.html
Copyright © 2011-2022 走看看