首页 技术 正文
技术 2022年11月15日
0 收藏 365 点赞 4,296 浏览 1611 个字

Lake Counting

Time Limit: 1000MS     Memory Limit: 65536K

Total Submissions: 17917     Accepted: 9069

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

思路

从任意的W开始,不停地把邻接的部分用’.’代替。 1次DFS后与初始的这个W连接的所有W就都被替
换成了’.’,因此直到图中不再存在W为止,总共进行DFS的次数就是答案了。 8个方向共对应了8种
状态转移,每个格子作为DFS的参数至多被调用一次,所以复杂度为O(8×N×M)=O(N×M)。

#include<stdio.h>#define MAX_N 105#define MAX_M 105char field[MAX_N][MAX_M];int N,M;void dfs(int x,int y){    int dx,dy,nx,ny;    field[x][y] = '.'; //将现在位置替换为'.';     for (dx = -1;dx < 2;dx++)  //遍历移动的8个方向    {        for (dy = -1;dy < 2;dy++)        {            nx = x + dx,ny = y + dy;            if (0 <= nx && nx < N && 0 <= ny && ny < M && field[nx][ny] == 'W')            {                                   //判断(nx,ny)是不是在园子内                dfs(nx,ny);            }        }    }}int main(){    int i,j,res = 0;    char tmp;    scanf("%d%d",&N,&M);    getchar();    for (i=0;i<N;i++)    {        for (j=0;j<M;j++)        {            scanf("%c",&field[i][j]);            if (j==M-1)            {                scanf("%c",&tmp);            }        }    }    for (i = 0;i < N;i++)    {        for (j = 0;j < M;j++)        {            if (field[i][j] == 'W')            {                dfs(i,j);                res++;            }        }    }    printf("%d\n",res);    return 0;}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,112
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,585
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,431
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,203
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,838
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,922