• 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 ;
    }
  • 相关阅读:
    POJ 1019 组合计数
    POJ 3252 组合计数
    SPJ 与 Student 数据库的创建于数据插入
    POJ 1496 POJ 1850 组合计数
    Java常用类库--大数处理类
    POJ 2492 并查集应用的扩展
    POJ 3268 双向Dijkstra
    线段树(带删除节点)
    西工大10级保研机试 柱状图
    KMP专场 POJ
  • 原文地址:https://www.cnblogs.com/yi-ye-zhi-qiu/p/7646071.html
Copyright © 2020-2023  润新知