zoukankan      html  css  js  c++  java
  • Avoid The Lakes(躲开湖水区)

    题目大意:给出一个地图,地图中有湖水区和干燥区,求出湖水区的最大面积

    解决:纯BFS,不用剪枝

    #include <iostream>    
    #include <queue>
    using namespace std;
    int map[105][105];
    int n,m,k;
    int dx[]={1,-1,0,0};
    int dy[]={0,0,1,-1};
    struct node 
    {
        int x,y;
        node(){}
        node(int xx,int yy):x(xx),y(yy){}
    };
    int bfs(int x,int y)
    {
        queue<node> q;
        node t,tmp;
        int sum=1;
        map[x][y]=0;
        q.push(node(x,y));
        while( !q.empty() )
        {
            t=q.front();
            q.pop();
            for(int i=0;i<4;i++)
            {
               tmp=node(t.x+dx[i],t.y+dy[i]);
               if(tmp.x>=1 && tmp.x <= m && tmp.y >=1 && tmp.y <=n && map[tmp.x][tmp.y])
               {
                   map[tmp.x][tmp.y]=0;
                   sum++;
                   q.push(tmp); 
               }
            }
        }
        return sum;
    }
    
    int main()
    {
        scanf("%d%d%d",&m,&n,&k);
        int a,b;
        while( k-- )
        {
            scanf("%d%d",&a,&b);
            map[a][b]=1;
        }
        
        int i,j,Max=-1,t;
        for(i=1;i<=m;i++)
          for(j=1;j<=n;j++)
            if(map[i][j]){ t = bfs(i,j); if(Max < t)Max=t; }
        printf("%d\n",Max);    
        system("pause");
        return 0;
    }
    

  • 相关阅读:
    面向对象高级
    面向对象基础总结
    面向对象基础剩余
    组合和封装
    继承与派生
    面向对象
    4.10
    4.9
    常用模块
    【转】CentOS: 开放80、22、3306端口操作
  • 原文地址:https://www.cnblogs.com/hpustudent/p/2166594.html
Copyright © 2011-2022 走看看