描述
在一个矩形区域内,有些地方有水,有些地方没水。所有相邻的有水的地方会共同组成一个水洼,小蝌蚪想在这块区域中找到一个最大的水洼来安家。
输入
有多组输入数据,每组第一行包含两个正整数n,m(n,m<=100),接下来n行,每行m个字符,“.”表示有水,“#”表示没水。
输出
对于每组输入数据输出一行,包含一个整数,表示最大的水洼的面积。
样例输入
3 3
###
###
##.
2 3
#..
..#
3 3
##.
#..
.##
样例输出
1
4
3
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
#define SIZE 100
char map[SIZE][SIZE];
bool visited[SIZE][SIZE];
struct node{
int x,y;
};
int BFS(int n,int m){
int i,j,count,max=0;
node tmp,p;
queue<node> path;
for (i=0;i<n;i++)
for (j=0;j<m;j++){
if (map[i][j]=='#')
visited[i][j]=true;
else if (map[i][j]=='.')
visited[i][j]=false;
}
for (i=0;i<n;i++)
for (j=0;j<m;j++)
if (!visited[i][j]){
count=0;
tmp.x=i;
tmp.y=j;
path.push(tmp);
visited[tmp.x][tmp.y]=true;
while (!path.empty()){
p=path.front();
count++;
path.pop();
if (p.x-1>=0&&!visited[p.x-1][p.y]){
tmp.x=p.x-1;
tmp.y=p.y;
path.push(tmp);
visited[tmp.x][tmp.y]=true;
}
if (p.x+1<n&&!visited[p.x+1][p.y]){
tmp.x=p.x+1;
tmp.y=p.y;
path.push(tmp);
visited[tmp.x][tmp.y]=true;
}
if (p.y-1>=0&&!visited[p.x][p.y-1]){
tmp.x=p.x;
tmp.y=p.y-1;
path.push(tmp);
visited[tmp.x][tmp.y]=true;
}
if (p.y+1<m&&!visited[p.x][p.y+1]){
tmp.x=p.x;
tmp.y=p.y+1;
path.push(tmp);
visited[tmp.x][tmp.y]=true;
}
}
if (count>max)
max=count;
}
return max;
}
int main(){
int n,m,i,j;
while (scanf("%d%d",&n,&m)!=EOF){
getchar();
for (i=0;i<n;i++){
for (j=0;j<m;j++)
scanf("%c",&map[i][j]);
getchar();
}
printf("%d\n",BFS(n,m));
}
return 0;
}
赤裸裸的广搜题。~~嘿嘿,第一次写,AC了很开心。~。