| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 38395 | Accepted: 19066 |
Description
Given a diagram of Farmer John's field, determine how many ponds he has.
Input
* 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
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题意:计算出湖的数目;
思路:题目中指出是八个方向的搜索,用深搜枚举八个方向计算出湖的数目。
#include<stdio.h>
char Map[110][110];//建图和标记两个功能
int dir[8][2] = {0,1,1,1,1,0,1,-1,0,-1,-1,-1,-1,0,-1,1};//八个方向
int r,c;
void dfs(int x,int y)
{
int i;
for(i = 0; i < 8; i ++)//枚举八个方向
{
int tx = x + dir[i][0];
int ty = y + dir[i][1];
if(tx<r && tx>=0 && ty<c && ty>=0 && Map[tx][ty]=='W')
{
Map[tx][ty] = '.';//标记已搜过
dfs(tx,ty);
}
}
}
int main()
{
int i,j,k;
while(~scanf("%d%d",&r,&c))
{
for(i = 0; i < r; i ++)
scanf("%s",Map[i]);
int sum = 0;
for(i = 0; i < r; i ++)
{
for(j = 0; j < c; j ++)
{
if(Map[i][j]=='W')//一趟搜索得到一个湖
{
dfs(i,j);
sum++;
}
}
}
printf("%d\n",sum);
}
return 0;
}
4778




被折叠的 条评论
为什么被折叠?



