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

解决:纯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;
}

 

posted on 2011-09-04 18:00  猿类的进化史  阅读(267)  评论(0编辑  收藏  举报