POJ2386总结

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.

10 12
W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.

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;
}

反思

#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);
}
}

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);
}
}

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”]