Lake Counting
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 30414   Accepted: 15195

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.

Source

[Submit]   [Go Back]   [Status]   [Discuss]

感觉自己一直没有系统的训练,从网上买了一本挑战程序设计竞赛,开始挨着刷题,这是第一道。

#include <iostream>
#include <cstdio> #define L 120 using namespace std; int n,m;
char a[L][L];
int dx[]={,,,,,-,-,-};
int dy[]={,,,-,-,-,,}; void dfs(int x,int y){
a[x][y]='.';
for(int i=;i<;i++){ int t=x+dx[i];
int t2=y+dy[i];
if(a[t][t2]=='W' && t>= && t<n && t2>= &&t2<m){
dfs(t,t2);
}
}
} int main()
{
int cou=;
while(~scanf("%d %d",&n,&m)){
getchar();
cou=;
for(int i=;i<n;i++){
for(int j=;j<m;j++){
scanf("%c",&a[i][j]);
}
getchar();
}
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(a[i][j]=='W'){
dfs(i,j);
cou++;
}
}
}
printf("%d\n",cou); }
return ;
}

Lake Counting_深度搜索_递归的更多相关文章

  1. 【动态规划】skiing_深度搜索_动态规划

    问题 B: [动态规划]skiing 时间限制: 1 Sec  内存限制: 128 MB提交: 28  解决: 11[提交][状态][讨论版] 题目描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪 ...

  2. #C++初学记录(深度搜索#递归)

    深度搜索 走地图的题目是深度搜索里比较容易理解的题目,更深层次的是全排列和七皇后等经典题目,更加难以理解,代码比较抽象. 题目:红与黑 蒜厂有一间长方形的房子,地上铺了红色.黑色两种颜色的正方形瓷砖. ...

  3. 深度优先搜索(DFS)递归形式改为非递归形式

    DFS将递归改为非递归这个方法的需求来自于一道三维积木组合的题目,还在苦苦调试中,暂且不提. 普通的认识对于递归向非递归的转化无非是使用栈,但是结合到深度搜索如何将栈很好利用,如何很好保存现场,都不是 ...

  4. #C++初学记录(算法测试2019/5/5)(深度搜索)

    深度搜索:Oil Deposits GeoSurvComp地质调查公司负责探测地下石油储藏. GeoSurvComp现在在一块矩形区域探测石油,并把这个大区域分成了很多小块.他们通过专业设备,来分析每 ...

  5. F - 蜘蛛牌(深度搜索)

    Problem Description 蜘蛛牌是windows xp操作系统自带的一款纸牌游戏,游戏规则是这样的:只能将牌拖到比她大一的牌上面(A最小,K最大),如果拖动的牌上有按顺序排好的牌时,那么 ...

  6. 双卡双待支持双电池 夏新N808深度评测_夏新手机评测-泡泡网

    双卡双待支持双电池 夏新N808深度评测_夏新手机评测-泡泡网 双卡双待支持双电池 夏新N808深度评测

  7. 算法基础_递归_求杨辉三角第m行第n个数字

    问题描述: 算法基础_递归_求杨辉三角第m行第n个数字(m,n都从0开始) 解题源代码(这里打印出的是杨辉三角某一层的所有数字,没用大数,所以有上限,这里只写基本逻辑,要符合题意的话,把循环去掉就好) ...

  8. 题目--oil Deposits(油田) 基础DFS(深度搜索)

    上一次基本了解了下BFS,这次又找了个基本的DFS题目来试试水,DFS举个例子来说就是 一种从树的最左端开始一直搜索到最底端,然后回到原端再搜索另一个位置到最底端,也就是称为深度搜索的DFS--dep ...

  9. 2018ICPC徐州区域赛网络赛B(逆序枚举或者正序深度搜索)

    #include<bits/stdc++.h>using namespace std;int n,m,k,l;int x[1007],y[1007],z[1007];int dp[1007 ...

随机推荐

  1. ios修改textField的placeholder的字体颜色和大小

    textField.placeholder = @"username is in here!"; [textField setValue:[UIColor redColor] fo ...

  2. SQL 语句-partition by

    /****** ******/ 初始化数据 create table employee (empid int ,deptid int ,salary decimal(10,2)) insert int ...

  3. SqlServer中字符串和日期类型的转换

    SQL Server Date 函数 定义和用法 CONVERT() 函数是把日期转换为新数据类型的通用函数. CONVERT() 函数可以用不同的格式显示日期/时间数据. 语法 CONVERT(da ...

  4. Centos ftp服务器安装配置

    yum install vsftpd [root@localhost ftp]# /sbin/service vsftpd restart 查看FTP目录 # more /etc/passwd|gre ...

  5. Android应用中返回键的监听及处理

    MainActivity: package com.testnbackpressed;  import android.os.Bundle;  import android.view.KeyEvent ...

  6. HDU HDU1558 Segment set(并查集+判断线段相交)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1558 解题报告:首先如果两条线段有交点的话,这两条线段在一个集合内,如果a跟b在一个集合内,b跟c在一 ...

  7. iOS开发——多线程篇——NSThread

    一.基本使用1.创建和启动线程一个NSThread对象就代表一条线程 创建.启动线程NSThread *thread = [[NSThread alloc] initWithTarget:self s ...

  8. C# 游戏服务器框架

    http://www.supersocket.net/ http://blog.csdn.net/zhuweisky/article/details/9055989 http://blog.csdn. ...

  9. leetcode 74. Search a 2D Matrix

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  10. [Leetcode19] Remove Nth Node From End of List

    视频讲解  http://v.youku.com/v_show/id_XMTY1MTMzNjAyNA==.html (1)定义两个指针 ListNode fast = head; ListNode s ...