链接:http://poj.org/problem?id=2386

Lake Counting

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 24263 Accepted: 12246

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

大意——给你一个n*m的矩形网格。每一个格子里面要么是‘W’。要么是‘.’,它们分别表示水和旱地。

如今要你计算出有多少个池塘。

每一个池塘由若干个水组成,水能连接的方向有8个,仅仅要是能连接到的水都属于一个池塘。

思路——一个简单的DFS题。我们将所有的网格所有遍历一次,假如我们找到一个池塘的源头,就能够进行一次计数。而且将眼下找到的池塘源头标记为‘.’,那么下一次就不会反复訪问了。再从八个方向进行深搜,将能到达相邻的‘W’所有标记,以免反复訪问。这样最后得到的计数就是答案。

复杂度分析——时间复杂度:O(n*m),空间复杂度:O(n*m)

附上AC代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <iomanip>
#include <ctime>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <map>
//#pragma comment(linker, "/STACK:102400000, 102400000")
using namespace std;
typedef unsigned int li;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const double pi = acos(-1.0);
const double e = exp(1.0);
const double eps = 1e-8;
const int maxn = 105;
const int dir[8][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {-1, -1}, {1, -1}, {-1, 1}, {1, 1}}; // 8个方向
int n, m; // 矩形的行列
char mat[maxn][maxn]; // 矩形void dfs(int x, int y); // 深度优先搜索int main()
{ios::sync_with_stdio(false);while (~scanf("%d%d", &n, &m)){int cnt = 0;for (int i=0; i<n; i++)scanf("%s", mat[i]);for (int i=0; i<n; i++)for (int j=0; j<m; j++)if (mat[i][j] == 'W'){ // 找到池塘源头,计数并深搜cnt++;dfs(i, j);}printf("%d\n", cnt);}return 0;
}void dfs(int x, int y)
{mat[x][y] = '.'; // 訪问过了。标记for (int i=0; i<8; ++i) // 从八个方向找相邻的if (x+dir[i][0]>=0 && x+dir[i][0]<n && y+dir[i][1]>=0 &&y+dir[i][1]<m && mat[x+dir[i][0]][y+dir[i][1]]=='W')dfs(x+dir[i][0], y+dir[i][1]); // 找到相邻的,继续深搜
}

转载于:https://www.cnblogs.com/liguangsunls/p/7054945.html

POJ 2386 Lake Counting相关推荐

  1. poj 2386 Lake Counting

    poj   2386   Lake Counting                           题目链接:http://poj.org/problem?id=2386 题目大意:数湖. 题目 ...

  2. POJ 2386 Lake Counting [DFS]

    POJ 2386 Lake Counting 简单的DFS,用了stack代替递归,输入有问题,搞了蛮久,算法是没问题.所以以后一定要记得加上检查输入的那一步 然后对于点的定义以后就这么办吧 此外还有 ...

  3. POJ 2386 Lake Counting DFS水水

    http://poj.org/problem?id=2386 题目大意: 有一个大小为N*M的园子,雨后积起了水.八连通的积水被认为是连接在一起的.请求出院子里共有多少水洼? 思路: 水题~直接DFS ...

  4. DFS 之 poj 2386 Lake Counting

    // [11/1/2014 JmingS] /* 遍历整个图,找到 'W' 的点,对其周围八个点其进行深搜,若是 'W' 则用 '.' 替换. 最后,在遍历整个图的过程中,找到多少个 'W',即答案. ...

  5. POJ 2386(DFS)

    深度优先搜索属于图算法的一种,英文缩写为DFS即Depth First Search.其过程简要来说是对每一个可能的分支路径深入到不能再深入为止,而且每个节点只能访问一次. 举例说明之:下图是一个无向 ...

  6. Lake Counting POJ - 2386

    Due to recent rains, water has pooled in various places in Farmer John's field, which is represented ...

  7. DFS:深入优先搜索 POJ-2386 Lake Counting

    深度优先搜索是从最开始的状态出发,遍历所有可以到达的状态. 因此可以对所有的状态进行操作,或列举出所有的状态. Lake Counting POJ - 2386 Due to recent rains ...

  8. 信息学奥赛一本通(1249:Lake Counting)

    1249:Lake Counting 时间限制: 1000 ms         内存限制: 65536 KB 提交数: 9435     通过数: 4902 [题目描述] 题意:有一块N×M的土地, ...

  9. NUC1158 Lake Counting【DFS】

    Lake Counting 时间限制: 1000ms 内存限制: 65536KB 通过次数: 1总提交次数: 1 问题描述 Due to recent rains, water has pooled ...

最新文章

  1. 【翻译】A Next-Generation Smart Contract and Decentralized Application Platform
  2. linux存储--linux内存分配图(九)
  3. JZOJ__Day 9:【普及模拟】Square
  4. Tomcat系统架构
  5. 「SVN」svn:将服务器关闭后报错Unable to connect to a repository at URL xxx
  6. jquery三级联动模糊查询_jquery三级联动
  7. onethink二级导航调用
  8. Visual Studio 2019 16.3 正式发布,支持 .NET Core 3.0
  9. 无服务器–仅仅是构建现代应用程序的一种方法?
  10. Webpack基础之加载器
  11. MySQL事务隔离级别及场景测试
  12. 内存释放_把微信这两个开关关掉, 手机立马释放大量内存, 快去试试吧
  13. 地理编码与反地理编码
  14. C盘全面清理教程!彻底清理所有垃圾!
  15. 贯头山酒——中华酒文化的源头之一
  16. 苹果手机咋用计算机,苹果手机怎么通过usb连接电脑上网
  17. 白话大数据--Hash分片
  18. GBase 8s分布式功能之异地容灾
  19. 商米机V2和V1内置打印机说明
  20. 什么是IP地址?IPV6和IPV4的区别-一个初学小白的笔记

热门文章

  1. 正则表达式的\b与\B总结
  2. CentOS上安装MyCat-MySQL
  3. struts+swfupload实现批量图片上传(上):swfupload
  4. js中写java集合代码,JS实现JAVA的List功能
  5. matplotlib plt.subplot
  6. GDB下查看内存命令(x命令)
  7. 小心使用STL中map的[]操作符
  8. 如何将二维数组作为函数的参数传递
  9. vue animation css实现左右折叠面板
  10. 用户画像是怎么生成出来的?