POJ3620 Avoid The Lakes

Avoid The Lakes
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7521   Accepted: 3958

Description

Farmer John's farm was flooded in the most recent storm, a fact only aggravated by the information that his cows are deathly afraid of water. His insurance agency will only repay him, however, an amount depending on the size of the largest "lake" on his farm.

The farm is represented as a rectangular grid with N (1 ≤ N ≤ 100) rows and M (1 ≤ M ≤ 100) columns. Each cell in the grid is either dry or submerged, and exactly K (1 ≤ K ≤ N × M) of the cells are submerged. As one would expect, a lake has a central cell to which other cells connect by sharing a long edge (not a corner). Any cell that shares a long edge with the central cell or shares a long edge with any connected cell becomes a connected cell and is part of the lake.

Input

* Line 1: Three space-separated integers: NM, and K
* Lines 2..K+1: Line i+1 describes one submerged location with two space separated integers that are its row and column: R and C

Output

* Line 1: The number of cells that the largest lake contains. 

Sample Input

3 4 5
3 2
2 2
3 1
2 3
1 1

Sample Output

4

Source


題目大意:農夫約翰有一塊N行M列的田地,這些地分爲大小相同的格子,每個格子爲1個單位(也就是1*1),這些格子中有K塊是有水的,輸入K行,每行兩個數代表x和y,這點表示有水,請求出最大面積的湖有多少個格子組成。


思路,先把“地圖”構成,有水的標記爲true,否則標記爲false,然後進行遞歸,

代碼如下:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define MAXN 400
using namespace std;

bool mp[MAXN][MAXN];   //記錄農田情況
int vis[MAXN][MAXN];   //如果這點沒有遍歷過爲0,否則從1開始依次+1
int N,M,K;
int cot;

void dfs(int r,int c)
{
    if(r>N || r<=0 || c<=0 || c>M) return;
    if(!mp[r][c] || vis[r][c]) return;
    if(!vis[r][c]  && mp[r][c]) {vis[r][c]=cot;cot++;}  //如果這點是水並且沒有遍歷過,就把計數值cot賦值進去
    dfs(r+1,c);    //進行四個方向的遍歷
    dfs(r-1,c);
    dfs(r,c+1);
    dfs(r,c-1);
}

int main()
{
    memset(mp,0,sizeof(mp));
    memset(vis,0,sizeof(vis));
    scanf("%d%d%d",&N,&M,&K);
    while(K--)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        mp[x][y]=true;    //如果這點有水,記爲true
    }
    for(int i=1;i<=N;i++)
    {
        for(int j=1;j<=M;j++)
        {
            cot=1;
            if((!vis[i][j]) && (mp[i][j])) dfs(i,j);   //如果這點沒有遍歷過,且爲true,從這點開始dfs
        }
    }
    int ans=-1;
    for(int i=1;i<=N;i++)
    {
        for(int j=1;j<=M;j++)
        {
            if(vis[i][j]>ans) ans=vis[i][j];   //求出最大值
        }
    }

    printf("%d\n",ans);
    return 0;
}

發佈了50 篇原創文章 · 獲贊 2 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章