Lake Counting -- DFS(深搜)

Time Limit: 1000MS

Memory Limit: 65536K
Total Submissions: 22424

Accepted: 11300

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

USACO 2004 November

解题分析:

首先这道题目设计几个问题:

第一个就是怎么可以确定几个w是属于一块的。

第二个怎么搜索。

第三个怎么确保不会搜索重复的。

那么第一个和第二个是一个问题,就是深度优先搜索,然后,利用递归的方法,每次对于一个点是否有8个方向,如果有的话,继续8个方向的搜索,直到到达边界为止。

还有怎么确定不重复,那么就是我把每次找到的w都变味‘  . ‘ 就可以了。

具体看代码。

代码:

#include <iostream>

#include <cstdio>

#define MAXN 102

using namespace std;

char map[MAXN][MAXN];

int N, M;

int DG(int i, int j) {

if(i < 0 || i >= N || j < 0 || j >= M)

return 0;

if(map[i][j] == ‘.‘)

return 0;

map[i][j] = ‘.‘;

return 1 + DG(i-1, j-1) + DG(i-1, j) + DG(i-1, j+1)

+ DG(i, j-1) + DG(i, j+1) + DG(i+1, j-1) +

DG(i+1, j) + DG(i+1, j+1);

}

int main()

{

cin >> n >> m;

char flag;

for(int i = 0; i < n; i++) {

for(int j = 0; j < m; j++) {

cin >> flag;

map[i][j] = flag;

}

}

int count = 0;

for(int i = 0; i < n; i++) {

for(int j = 0; j < m; j++) {

if(DG(i, j) != 0)

count++;

}

}

cout << count << endl;

return 0;

}

时间: 2024-08-01 14:56:04

Lake Counting -- DFS(深搜)的相关文章

CodeM美团点评编程大赛初赛B轮 黑白树【DFS深搜+暴力】

[编程题] 黑白树 时间限制:1秒 空间限制:32768K 一棵n个点的有根树,1号点为根,相邻的两个节点之间的距离为1.树上每个节点i对应一个值k[i].每个点都有一个颜色,初始的时候所有点都是白色的. 你需要通过一系列操作使得最终每个点变成黑色.每次操作需要选择一个节点i,i必须是白色的,然后i到根的链上(包括节点i与根)所有与节点i距离小于k[i]的点都会变黑,已经是黑的点保持为黑.问最少使用几次操作能把整棵树变黑. 输入描述: 第一行一个整数n (1 ≤ n ≤ 10^5) 接下来n-1

A Knight&#39;s Journey(DFS)深搜

A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33634 Accepted: 11450 Description Background The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey around

hdu1010-Tempter of the Bone DFS深搜入门题+奇偶剪枝

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 69699    Accepted Submission(s): 19176 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

DFS深搜-Red and Black

深搜,从一点向各处搜找到所有能走的地方. Problem Description There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on

dfs深搜

dfs深搜 什么是深搜? 百度百科:按照一定的顺序.规则,不断去试探,直到找到问题的解,试完了也没有找到解,那就是无解,试探时一定要试探完所有的情况(实际上就是穷举) 个人理解:暴力穷举每一种状态 它有什么好处? 容易理解 骗分利器 好写 它有什么弊端? 慢.毕竟是穷举每一种状态 一搜到底.对于递归次数非常多的话,dfs会非常浪费时间,甚至有可能会爆栈 如何实现? 算法流程图如下: #include <iostream> #include <cstdio> void dfs(int

【DFS深搜初步】HDOJ-2952 Counting Sheep、NYOJ-27 水池数目

[题目链接:HDOJ-2952] Counting Sheep Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2476    Accepted Submission(s): 1621 Problem Description A while ago I had trouble sleeping. I used to lie awake,

HDU 1045 Fire Net【DFS深搜】

Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9880    Accepted Submission(s): 5739 Problem Description Suppose that we have a square city with straight streets. A map of a city is a

HDU-1016 Prime Ring Problem(DFS深搜+打表)

题目回顾(HDU-1016): Prime Ring Problem Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime.Note: the number

HDU_1241 Oil Deposits(DFS深搜)

Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square pl