155. [usaco nov07] largest lake
★Input file:lake.in
Output file:lake.out
Simple 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;}