zoukankan      html  css  js  c++  java
  • (简单) POJ 1562 Oil Deposits,BFS。

      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.
     
      就是BFS了,水题吧。
     
    代码如下:
    #include<iostream>
    #include<cstring>
    
    using namespace std;
    
    int vis[110][110];
    int ans;
    int M,N;
    int fir,las,que[10005];
    
    bool judge(int x,int y)
    {
        if(x<=0||y<=0||x>M||y>N)
            return 0;
    
        if(vis[x][y]==0)
            return 0;
    
        return 1;
    }
    
    void bfs(int x,int y)
    {
        int temp,t1,t2;
    
        que[las++]=x*1000+y;
        vis[x][y]=0;
    
        while(las-fir)
        {
            temp=que[fir++];
    
            t1=temp/1000;
            t2=temp%1000;
    
            --t1;
            if(judge(t1,t2))
            {
                vis[t1][t2]=0;
                que[las++]=t1*1000+t2;
            }
            t1+=2;
            if(judge(t1,t2))
            {
                vis[t1][t2]=0;
                que[las++]=t1*1000+t2;
            }    
            --t1;
            --t2;
            if(judge(t1,t2))
            {
                vis[t1][t2]=0;
                que[las++]=t1*1000+t2;
            }    
            t2+=2;
            if(judge(t1,t2))
            {
                vis[t1][t2]=0;
                que[las++]=t1*1000+t2;
            }
            t2-=2;
            --t1;
            if(judge(t1,t2))
            {
                vis[t1][t2]=0;
                que[las++]=t1*1000+t2;
            }    
            t1+=2;
            if(judge(t1,t2))
            {
                vis[t1][t2]=0;
                que[las++]=t1*1000+t2;
            }    
            t2+=2;
            if(judge(t1,t2))
            {
                vis[t1][t2]=0;
                que[las++]=t1*1000+t2;
            }    
            t1-=2;
            if(judge(t1,t2))
            {
                vis[t1][t2]=0;
                que[las++]=t1*1000+t2;
            }
        }
    }
    
    void slove()
    {
        ans=0;
        fir=las=0;
        
        for(int i=1;i<=M;++i)
            for(int j=1;j<=N;++j)
                if(vis[i][j])
                {
                    ++ans;
                    bfs(i,j);
                }
    }
    
    int main()
    {
        ios::sync_with_stdio(false);
        
        char c;
    
        for(cin>>M>>N;M;cin>>M>>N)
        {
            for(int i=1;i<=M;++i)
                for(int j=1;j<=N;++j)
                {
                    cin>>c;
                    vis[i][j]=c=='@'?1:0;
                }
    
            slove();
            cout<<ans<<endl;
        }
    
        return 0;
    }
    View Code
  • 相关阅读:
    屏蔽浏览器的脚本错误提示
    服务器定时重启计划任务
    卖程序的小女孩(转)
    会话状态已创建一个会话 ID,但由于响应已被应用程序刷新而无法保存它。
    Sesion空框架跳到登陆js
    poj 1679 The Unique MST (次小生成树 模版)
    hdu 3488 Tour (km 二分图 最小权)
    sdut 2401 最大矩形面积
    poj 3422 Kaka's Matrix Travels (最大费用流(最小费用最大流)+++拆点)
    博弈论 简介
  • 原文地址:https://www.cnblogs.com/whywhy/p/4229931.html
Copyright © 2011-2022 走看看