zoukankan      html  css  js  c++  java
  • HDU--杭电--1241--Oil Deposits--广搜

    Oil Deposits

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


    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 <iostream>
    #include <queue>
    using namespace
    std;
    int
    n,m,visit[111][111],xx[8][2]={{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};  //xx数组是用来记录方向的,到时候就只要直接加就好了
    char
    map[111][111];
    struct
    ssss
    {

        int
    x,y;  //因为要存的是坐标,没有可以装两个变量的队列,所以先放结构体打包再加入队列
    }
    ss;
    queue<ssss> q,qq;  //结构体类型的队列,q是要用到,qq是用来初始化q的
    void
    bfs()
    {

        while
    (!
    q.empty())
        {

            ss=q.front();q.pop();  //用ss取出队首元素并删除它
            int
    X=ss.x,Y=ss.y;
            map[X][Y]='*';visit[X][Y]=0;  //在map里面把当前坐标从@变成×,visit标记为已经访问
            for
    (int
    i=0;i<8;i++)
            {

                int
    x=X+xx[i][0];  //8个方向一个一个来,嘿嘿
                int
    y=Y+xx[i][1];
                if
    (
    x>=0&&x<n&&y>=0&&y<m)  //判断是否在范围内
                    if
    (
    visit[x][y]&&map[x][y]=='@')  //判断是否访问过和是否为油田@
                    {

                        visit[x][y]=0;  //标记已经访问
                        ss.x=x,ss.y=y;q.push(ss);  //先打包,再入队
                    }
            }
        }
    }

    int
    main (void)
    {

        int
    i,j,k,l,s;
        while
    (
    cin>>n>>m&&(n||m))
        {

            for
    (
    i=0;i<n;i++)
                for
    (
    j=0;j<m;j++)
                    cin>>map[i][j],visit[i][j]=1;
                q=qq;
                for
    (
    i=s=0;i<n;i++)  //全图搜索
                    for
    (
    j=0;j<m;j++)
                        if
    (
    map[i][j]=='@')  //当找到油田@
                            s++,ss.x=i,ss.y=j,q.push(ss),bfs();  //油田群数量加一,打包,入队
                        cout<<s<<endl;
        }

        return
    0;
    }

  • 相关阅读:
    浮动
    关于在windows下同时安装两个mysql,并用Navicat工具同时连接
    关于Springboot项目打包部署运行,命令行运行jar提示没有主清单属性
    If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.(个人笔记)
    关于IDEA解决默认的javacompile以及Language level自动默认jdk1.5的问题
    你真的精通JavaWeb吗?
    Java8新特性(个人笔记待续)
    Netty(个人笔记待续)
    初识网络编程(1)
    走进shiro
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3228634.html
Copyright © 2011-2022 走看看