[Usaco nov07] the largest lake

Source: Internet
Author: User

155. [usaco nov07] largest lake

★Input file:lake.inOutput file:lake.outSimple comparison
Time Limit: 1 s memory limit: 128 MB

Translated by cmykrgb123

Description

In a recent rainstorm, Farmer John's farm was drowned, forming lakes. What's more terrible is that his cows are most afraid of water. Fortunately, his insurance company will give him a lot of compensation, which depends on the size of the largest lake on his farm.

His farm can be depicted as a grid of N (1 ≤ n ≤ 100) rows M (1 ≤ m ≤ 100) columns. Each grid is either land or lake water. It is known that there are K (1 ≤ k ≤ n × m) grids with water. Each grid of water can belong to a lake only when its sides are adjacent, and its vertices are not adjacent.

Calculate the size of the largest lake on his farm.

Input

  • Row 1st: Three integers: n, m, K
  • 2nd. k + 1 row: two integers in the I + 1 row, indicating the location of water in the r row, column C: R, c

Output

  • Row 1st: the largest lake size (the number of grids that comprise it)

Sample Input

3 4 53 22 23 12 31 1

Sample output

4
 
 
Simple BFs.
#include<cstdio>#include<queue>using namespace std;int map[110][110];bool vis[110][110];int dx[]={0,0,1,-1};int dy[]={1,-1,0,0};int n,m;struct node{    int x,y;}s_pos;int ans;void bfs(int sx,int sy){    queue<node> q;    s_pos.x=sx; s_pos.y=sy;    int sum=1;    vis[sx][sy]=true;    q.push(s_pos);    while(!q.empty()){        node now = q.front();q.pop();        if(sum>ans) ans=sum;        for(int i=0;i<4;i++){            node next = now;            next.x+=dx[i];  next.y+=dy[i];            if(!vis[next.x][next.y]&&map[next.x][next.y]==1){                sum++;                vis[next.x][next.y]=true;                q.push(next);            }        }    }}int main(){    freopen("lake.in","r",stdin);    freopen("lake.out","w",stdout);    int n,m,k;    int a,b;    scanf("%d%d%d",&n,&m,&k);    for(int i=0;i<k;i++){        scanf("%d%d",&a,&b);        map[a][b]=1;    }    for(int i=1;i<=n;i++){       for(int j=1;j<=m;j++)        if(map[i][j]==1&&!vis[i][j]){             bfs(i,j);        }    }    printf("%d\n",ans);    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.