POJ-3620:
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: N, M, 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
1 1 * 3 5 *@*@* **@** *@*@* 1 8 @@****@* 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ 0 0
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)