Lake Counting
Description
Due to recent rains, water has pooled in various places in Farmer John’s field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water (‘W’) or dry land (‘.’). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors.
Given a diagram of Farmer John’s field, determine how many ponds he has.
Input
* Line 1: Two space-separated integers: N and M
* Lines 2..N+1: M characters per line representing one row of Farmer John’s field. Each character is either ‘W’ or ‘.’. The characters do not have spaces between them.
Output
* Line 1: The number of ponds in Farmer John’s field.
Sample Input
10 12 W........WW. .WWW.....WWW ....WW...WW. .........WW. .........W.. ..W......W.. .W.W.....WW. W.W.W.....W. .W.W......W. ..W.......W.
Sample Output
3
Hint
OUTPUT DETAILS:
There are three ponds: one in the upper left, one in the lower left,and one along the right side.
我的代码
#include<stdio.h>
int map[101][101];//记录地图数据,0代表地面,1代表水面
int flag[101][101]={0};//记录此位置是否已经被搜索过
int num=0;//记录水塘的总个数
int n,m;
int bound(int x,int y)
//判断所给的坐标是否越界
{
if (x>=0&&x<n&&y>=0&&y<m) return 1;
else return 0;
}
int search(int x,int y)
//从指定的位置开始搜索直到找到整个池塘
{
//八个if分别是八个方向
if (bound(x-1,y-1))
{
if (!flag[x-1][y-1])
{
flag[x-1][y-1]=1;
if (map[x-1][y-1])
//如果是水面就沿着整个位置展开新的搜索,下同
{
search(x-1,y-1);
}
}
}
if (bound(x-1,y))
{
if (!flag[x-1][y])
{
flag[x-1][y]=1;
if (map[x-1][y])
{
search(x-1,y);
}
}
}
if (bound(x-1,y+1))
{
if (!flag[x-1][y+1])
{
flag[x-1][y+1]=1;
if (map[x-1][y+1])
{
search(x-1,y+1);
}
}
}
if (bound(x,y-1))
{
if (!flag[x][y-1])
{
flag[x][y-1]=1;
if (map[x][y-1])
{
search(x,y-1);
}
}
}
if (bound(x,y+1))
{
if (!flag[x][y+1])
{
flag[x][y+1]=1;
if (map[x][y+1])
{
search(x,y+1);
}
}
}
if (bound(x+1,y-1))
{
if (!flag[x+1][y-1])
{
flag[x+1][y-1]=1;
if (map[x+1][y-1])
{
search(x+1,y-1);
}
}
}
if (bound(x+1,y))
{
if (!flag[x+1][y])
{
flag[x+1][y]=1;
if (map[x+1][y])
{
search(x+1,y);
}
}
}
if (bound(x+1,y+1))
{
if (!flag[x+1][y+1])
{
flag[x+1][y+1]=1;
if (map[x+1][y+1])
{
search(x+1,y+1);
}
}
}
}
int main()
{
char c;
scanf("%d%d",&n,&m);
c=getchar();
for (int i=0;i<n;i++)
{
for (int j=0;j<m;j++)
{
c=getchar();
if (c=='W') map[i][j]=1;
else if (c=='.') map[i][j]=0;
}
c=getchar();
}
for (int i=0;i<n;i++)
{
for (int j=0;j<m;j++)
{
if (!flag[i][j])
//如果没有访问过这个位置
{
if (map[i][j])
//如果此处是水面
{
num++;//池塘数量++
search(i,j);
}
}
}
}
printf("%d",num);
return 0;
}
我的思路是遍历所有未访问过的点(flag[i][j]=0),如果是水面,就说明有一个新发现的水塘(水塘个数++),并且对其进行搜索,直到探索完整个这个池塘。一趟遍历下来之后就可以得到水塘的总个数。
反思
上网搜了一下其他人的做法,同样是dfs,但是明显思路比我清晰:
#include <stdio.h>
#define maxn 107
int n,m;
char g[maxn][maxn];
int dir[10][2] = {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,0},{1,-1},{1,1}};
void dfs(int x,int y){
g[x][y] = '.';//此行代码意义重大,相当于将其置为已访问状态
for(int i = 0;i < 8;i++) {
int dx = x+dir[i][0];
int dy = y+dir[i][1];
if(dx>n||dx<1||dy<1||dy>m)
continue;
if(g[dx][dy] == '.')
continue;
dfs(dx,dy);
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF) {
int ans = 0;
for(int i = 1;i <= n;i++)
scanf("%s",g[i]+1);
int cnt = 0;
for(int i = 1;i <= n;i++)
for(int j = 1;j <= m;j++) {
if(g[i][j] == 'W') {
ans++;
dfs(i,j);//把所有和该点相邻的W都变成.
}
}
printf("%d\n",ans);
}
return 0;
}
与我不同的地方主要是,我是新开辟了空间来记录地图上未访问过的位置,而上面的解法是直接修改地图。对比起来我新开了一个数组,浪费了不必要的空间。
另外我的代码中表示八个方向的搜索也很愚蠢。在查询网络上其它人的代码之后,我找到了两种做法:
一是上面的代码使用的将八个方向存储在数组内,调用时遍历方向数组。
int dir[10][2] = {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,0},{1,-1},{1,1}};
void dfs(int x,int y){
g[x][y] = '.';//此行代码意义重大,相当于将其置为已访问状态
for(int i = 0;i < 8;i++) {
int dx = x+dir[i][0];
int dy = y+dir[i][1];
if(dx>n||dx<1||dy<1||dy>m)
continue;
if(g[dx][dy] == '.')
continue;
dfs(dx,dy);
}
}
二是直接遍历方向(-1,0,1)。
for (int dx = -1; dx <= 1; dx++)//循环遍历连通的8个方向
{
for (int dy = -1; dy <= 1; dy++)
{
int nx = x + dx, ny = y + dy;//向x方向移动dx,向y方向移动dy,移动的结果为(nx,ny)
if (0 <= nx&&nx <= N && 0 <= ny&&ny <= M&&field[nx][ny] == 'W')//判断(nx,ny)是不是在园子里,以及是否有积水
dfs(nx, ny);
}
}
此外我的算法还有有一个问题:在主函数遍历过程中如果一个点是地面,即便已经访问过这个点,也没有能够及时地将其flag置为1,这样之后可能还是会访问这个点,可能会进行了不必要的遍历。
for (int i=0;i<n;i++)
{
for (int j=0;j<m;j++)
{
if (!flag[i][j])
//如果没有访问过这个位置
{
//此处应该添加一句 flag[i][j]=1;
if (map[i][j])
//如果此处是水面
{
num++;//池塘数量++
search(i,j);
}
}
}
}
[display-posts author=”Shopkeeper”]