题目描述
The Flood come from the border of the map, so tell me the area without flood...Do u think it's easy ?
输入描述
First T(<=100) as T case...
Then M(<=100),N(<=100) as the size of Map...
Then the map coming...
'#' as the wall and it's enough high to against flood
'.' as the land
输出描述
Show the number of the point without flood...
样例输入
2
3 3
.#.
#.#
.#.
4 4
####
#..#
#..#
####
比较easy的搜索题,有一种比较好的方法是在地图边缘添点,然后在(0,0)处开始搜索,记录被淹过的面积以及大坝的总长,最后相减得到未淹过的面积。当然也可以遍历每个未被访问的陆地,对每个点实行宽搜,如果宽搜中出现越界的判定,则说明未被大坝包围,否则记录在改次搜索中结点的总数,最后累加得到结果。
我的代码写的比较麻烦,对4个边界上的点进行深搜,并处理(将访问到的“.”--->"#"),最后统计地图中"."的个数并是结果了
代码如下:

Code
include<stdio.h>
#include<string.h>
int t,n,m;
char a[102][102];
int v[102][102];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int test(int i,int j)
{
return (i>=m||i<0||j>=n||j<0);
}
void process(int i,int j)
{
int k;
if(test(i,j))
return ;
if(v[i][j]||a[i][j]=='#')
return ;
if(a[i][j]=='.'){
a[i][j]='#';
v[i][j]=1;
for(k=0;k<4;k++)
process(i+dx[k],j+dy[k]);
}
}
void output()
{
int i,j,ans=0;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
if(a[i][j]=='.')
ans++;
printf("%d\n",ans);
}
int main()
{
int i,j;
scanf("%d",&t);
while(t--){
scanf("%d %d",&m,&n);
for(i=0;i<m;i++)
scanf("%s",a[i]);
memset(v,0,sizeof(v));
for(i=0;i<m;i++){
process(i,0);
process(i,n-1);
}
for(i=0;i<n;i++){
process(0,i);
process(m-1,i);
}
output();
}
}