POJ-3620

POJ-3620:

Avoid The Lakes
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6697 Accepted: 3595

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


分析:题目大意是:给出一个布尔矩阵,若矩阵上下左右四个方向中任意一个有1,都可视作连成“一块”,那么最大的一块能包含多少数字。

    解决办法:DFS求连通分量。(题目中数字成对给出,给人第一感觉就是并查集或者图中的边==)

    这道题目在做的时候,主要体会还是对连通分量的理解不够透彻。真正的代码部分实际上并不难。需要注意判断什么时候递归到头(主要是对越界,已访问过不能再次访    问的数据的处理)。


代码如下:

//POJ3620

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


int N,M,K,cnt;
bool table[120][120];
bool vis[120][120];


int dfs(int r,int c){
    if(r<1||c<1||r>N||c>M)  return cnt;
    if(vis[r][c]==true||table[r][c]==false)   return cnt;
    cnt++;
    vis[r][c]=true;
    dfs(r-1,c);
    dfs(r,c-1);
    dfs(r+1,c);
    dfs(r,c+1);
}
int main(){
    while(cin>>N>>M>>K){
        memset(table,false,sizeof(table));
        memset(vis,false,sizeof(vis));
        int f,r,maxcnt=0;
        for(int i=0;i<K;i++){
            cin>>f>>r;
            table[f][r]=true;
        }
        /*for(int i=1;i<=N;i++){
            for(int j=1;j<=M;j++)
                cout<<table[i][j];
            cout<<endl;
        }*/
        for(int i=1;i<=N;i++)
            for(int j=1;j<=M;j++)
                if(vis[i][j]==false&&table[i][j]==true){
                    maxcnt=max(maxcnt,dfs(i,j));
                    cnt=0;
                }
        cout<<maxcnt<<endl;
    }
    return 0;
}



实际上,这道题目和HDOJ上一道题目是一样的思路:

Oil Deposits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17207    Accepted Submission(s): 9909


Problem Description
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.
 

Input
The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket.
 

Output
For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.
 

Sample Input
1 1 * 3 5 *@*@* **@** *@*@* 1 8 @@****@* 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ 0 0
 

Sample Output
0 1 2 2
思路和上面是大致相同的,下面直接给出代码:


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;


const int maxn=105;
char pic[maxn][maxn];
int m,n,id[maxn][maxn];


void dfs(int i,int j,int id_){
    if(i<0||j<0||i>=m||j>=n)
        return;
    if(id[i][j]>0||pic[i][j]!='@')
        return;
    id[i][j]=id_;
    dfs(i-1,j-1,id_);
    dfs(i-1,j,id_);
    dfs(i-1,j+1,id_);
    dfs(i,j-1,id_);
    dfs(i,j+1,id_);
    dfs(i+1,j-1,id_);
    dfs(i+1,j,id_);
    dfs(i+1,j+1,id_);
}
int main(){
    while(scanf("%d%d",&m,&n)==2&&m!=0&&n!=0){
        memset(id,0,sizeof(id));
        for(int i=0;i<m;i++)
            scanf("%s",pic[i]);
        int cnt=0;
        for(int i=0;i<m;i++)
            for(int j=0;j<n;j++)
                if(pic[i][j]=='@'&&id[i][j]==0)
                    dfs(i,j,++cnt);
        cout<<cnt<<endl;
    }
    return 0;
}



实际上面代码中,连续使用8个dfs,在修改和编写的时候难度都比较大,直接用一个循环语句即可完成。(参见刘汝佳紫皮书“用DFS求连通块”一节,UVa 572)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值