zoukankan      html  css  js  c++  java
  • 广度搜索(2)

    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. 
     

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    const int maxn=150;
    char map[maxn][maxn];
    bool vis[maxn][maxn];
    int qx[maxn*maxn],qy[maxn*maxn];
    int dir[8][2]= {{1,0},{-1,0},{0,1},{0,-1},{-1,1},{-1,-1},{1,1},{1,-1}};
    int n,m,ans;

    void bfs()
    {
        int i,j;
        for(i=1;i<=n;i++)
        {
             for(j=1;j<=m;j++)
             {
                if(map[i][j]=='@'&&vis[i][j]==0)
                {
                   ans++;
                   int l=0,r=0;
                   qx[r]=i,qy[r++]=j;
                   vis[i][j]=1;
                   while(l<r)
                  {
                     int curx=qx[l],cury=qy[l++];
                     for(int t=0;t<8;t++)
                    {
                       int nx=curx+dir[t][0];
                       int ny=cury+dir[t][1];
                       if(nx>=1&&nx<=n&&ny>=1&&ny<=m&&!vis[nx][ny]&&map[nx][ny]=='@')
                      {
                          vis[nx][ny]=1;
                          qx[r]=nx,qy[r++]=ny;
                      }
                    }
                  }
                }
             }
         }
    }

    int main()
    {
        int i,j;
        while(scanf("%d%d",&n,&m)==2&&(n||m))
        {
           for(i=1;i<=n;i++)
           for(j=1;j<=m;j++)
            cin>>map[i][j];
            ans=0;
            memset(vis,0,sizeof(vis));
            bfs();
            printf("%d ",ans);
        }
        return 0;
    }

    深搜代码:

    int p[8][2]={{0,1},{0,-1},{1,0},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}};
    char ch[101][101];
    int m,n;
    void search(int x,int y)
    {
      int i,xx,yy;
      for(i=0;i<8;i++)
      {
      xx=x+p[i][0];yy=y+p[i][1];
      if(xx<0||xx>=m||yy<0||yy>=n) continue;
      if(ch[xx][yy]=='*')continue;
      ch[xx][yy]='*';
      search(xx,yy);               
      }    
    }

    int main()
    {
      int i,j,count;
      while(scanf("%d%d",&m,&n)&&m+n)
      {
      for(i=0;i<M;I++) scanf(?%s?,ch[i]);
      count=0; 
      for(i=0;i<M;I++)
      for(j=0;j<N;J++)
      {
      if(ch[i][j]=='@')
      {search(i,j);count++;}               
      }                    
      printf("%d ",count);      
      }   
    }
  • 相关阅读:
    (转)一文讲清TCP/IP 协议
    Java框架之Spring Boot和Spring Cloud区别
    php大力力 [006节]初步接触认识phpMyAdmin
    php大力力 [005节] php大力力简单计算器001
    php大力力 [004节]PHP常量MAMP环境下加载网页
    php大力力 [003节]php在百度文库的几个基础教程mac环境下文本编辑工具
    php大力力 [002节]mac php环境安装,mamp安装 ,phpMyAdmin启动
    php大力力 [001节]2015-08-21.php在百度文库的几个基础教程新手上路日记 大力力php 大力同学 2015-08-21 15:28
    C# DataSet与DataTable的区别和用法 ---转载
    【SqlServer】利用sql语句附加,分离数据库-----转载
  • 原文地址:https://www.cnblogs.com/chen9510/p/4705750.html
Copyright © 2011-2022 走看看