zoukankan      html  css  js  c++  java
  • kuangbin专题一:L题,HDU1241:Oil Deposits(连通块个数)

    HDU1241:Oil Deposits(连通块个数)

    kuangbin专题一:L题

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


    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
     
     题意:求连通块的个数(一个@相连 或者多个@相连成为一个连通块)
    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <algorithm>
    
    using namespace std ;
    
    #define maxn 150
    // 相连不是说只有上下左右四个方向  相连意思是一周圈八个方向 
    int dirx[] = {0 , 0 , 1 , -1 , -1 , -1 , 1,  1 } ;
    int diry[] = {-1 , 1 , 0 , 0 , -1 , 1 , -1 , 1 } ;
    int H , L ;
    bool visit[maxn][maxn] ;
    char map[maxn][maxn] ;
    
    bool check(int x , int y) {
        if(x>=1 && x<=H && y>=1 && y<=L) {
            return true ;
        }
        return false ;
    }
    
    void BFS(int x , int y ) {// 把所有 属于某一个连通块的所有子块  标记 
        int X , Y ;
        for(int i=0 ; i<8 ; i++) {
            X = x + dirx[i] ;
            Y = y + diry[i] ;
            if(check(X , Y ) && !visit[X][Y] && map[X][Y] == '@') {
                visit[X][Y] = true ;
                BFS(X , Y ) ;
            }
        }
        return;
    }
    
    int main() {
        while(~scanf("%d %d" , &H , &L)) {
            if(H==0 &&L==0)
                break ;
            memset(visit , false , sizeof(visit)) ;
    
            for(int i=1 ; i<=H ; i++) {
                for(int j=1 ; j<=L ; j++) {
                    scanf(" %c" ,&map[i][j] ) ;
                }
            }
            int sum = 0 ; // 统计连同块数目
            for(int i=1 ; i<=H ; i++) {
                for(int j=1 ; j<=L ; j++) {
                    if(!visit[i][j] && map[i][j] == '@') {// 遇到新块 
                        visit[i][j] = true ;
                        BFS( i , j  ) ;
                        sum ++ ;
                    }
                }
            }
            printf("%d
    " , sum ) ;
        }
        return 0 ;
    }
  • 相关阅读:
    RESTful API
    访问方式由http改为https curl:(51)
    java.lang.OutOfMemoryError: PermGen space
    liunx下tomcat启动 Cannot find ./catalina.sh
    Java-编译后出现$1.class、$2.class等多个class文件
    错误处理的返回--异常还是返回值
    ubuntu 上安装温度检测
    mysql5.6不能输入中文
    jmap在ubuntu上DebuggerException: Can't attach to the process
    tomcat-reload-与内存泄露
  • 原文地址:https://www.cnblogs.com/yi-ye-zhi-qiu/p/7646071.html
Copyright © 2011-2022 走看看