zoukankan      html  css  js  c++  java
  • hdu1241 bfs 模板

    Oil Deposits

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


    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

     
    对于我个人来说很重要的一题吧,每次bfs tle还是mle那么多次后,我终于发现症结所在,
    应该是在每个step入队的时候i标记,而非出队的时候标记,原因很简单,考虑相邻节点吧,如果S->A&&S->B&&A->B,这种情况就非常尴尬了,B会两次入队,感觉以后会有题目会要考虑这种的,拭目以待吧
    以前得了数据量小的便宜,晚上发现这个错误非常高兴
    回到这题,找同类吧,题目还是非常简单,感觉和省赛那题差不多,,,
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <cmath>
    #include <string>
    #include <stack>
    #include <queue>
    #include <algorithm>
    
    const int inf = (1<<31)-1;
    const int MAXN = 1e2+10;
    const int MMAXN = 5e4+10;
    using namespace std;
    
    struct step{
        int x;
        int y;
    };
    
    int n,m;
    queue<step>q;
    /*step q[MMAXN];
    int l,r;*/
    char G[MAXN][MAXN];
    int mov[8][2]={0,1,0,-1,1,0,-1,0,1,1,-1,-1,-1,1,1,-1};
    /*int mov[4][2]={0,1,1,0,1,-1,1,1};*/
    
    int  check(int x,int y){
        if(x<0||y<0||x>=n||y>=m)return 0;
        if(G[x][y]=='*')return 0;
        else return 1;
    }
    
    
    int main()
    {
        int tk;
        step t,fro;
        int nx,ny;
        while(scanf("%d%d",&n,&m),n){
            tk = 0;
            for(int i=0;i<n;i++){
                scanf("%s",G[i]);
            }
            for(int i=0;i<n;i++){
                for(int j=0;j<m;j++){
                    if(G[i][j]=='@'){
                            tk++;
                            t.x = i;
                            t.y = j;
                            q.push(t);
                            G[i][j]='*';
                            while(!q.empty()){
                                fro = q.front();
                                q.pop();
                               // G[fro.x][fro.y] = '*';
                                for(int i=0;i<8;i++){
                                    nx = fro.x+mov[i][0];
                                    ny = fro.y+mov[i][1];
                                    if(check(nx,ny)){
                                        t.x = nx;
                                        t.y = ny;
                                        q.push(t);
                                        G[nx][ny] = '*';
                                    }
                                }
                            }
                          /* G[i][j] = '*';
                            l = r = 0;
                            q[r++] = t;
                            while(l<r){
                                fro = q[l];
                                l++;
                               // G[fro.x][fro.y] = '*';
                                for(int i=0;i<8;i++){
                                    nx = fro.x+mov[i][0];
                                    ny = fro.y+mov[i][1];
                                    if(check(nx,ny)){
                                        t.x = nx;
                                        t.y = ny;
                                        q[r++] = t;
                                        G[nx][ny] = '*';
                                    }
                                }
                            }*/
                    }
                }
            }
            cout<<tk<<endl;
        }
        //cout << "Hello world!" << endl;
        return 0;
    }
    View Code
     
     
     
     
     
     
     
     
    在一个谎言的国度,沉默就是英雄
  • 相关阅读:
    Linux下安装jdk
    hadoop下载
    Java:xxx is not an enclosing class
    Android:Gradle报错——No resource found that matches the given name (at 'dialogCornerRadius' with value '?android:attr/dialogCornerRadius')
    TensorFlow:在PyCharm中配置TensorFlow
    Android:屏幕旋转
    Android:onActivityResult详解
    Android:Bundle类
    Android:ConstraintLayout完全解析
    Android:Android Studio生成签名文件,自动签名,以及获取SHA1和MD5值
  • 原文地址:https://www.cnblogs.com/EdsonLin/p/5433311.html
Copyright © 2011-2022 走看看