题目描述

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.

由于近期的降雨,雨水汇集在农民约翰的田地不同的地方。我们用一个NxM(1<=N<=100;1<=M<=100)网格图表示。每个网格中有水('W') 或是旱地('.')。一个网格与其周围的八个网格相连,而一组相连的网格视为一个水坑。约翰想弄清楚他的田地已经形成了多少水坑。给出约翰田地的示意图,确定当中有多少水坑。

输入输出格式

输入格式:

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.

第1行:两个空格隔开的整数:N 和 M 第2行到第N+1行:每行M个字符,每个字符是'W'或'.',它们表示网格图中的一排。字符之间没有空格。

输出格式:

Line 1: The number of ponds in Farmer John's field.

一行:水坑的数量

输入输出样例

输入样例#1:
复制

10 12
W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.
输出样例#1: 复制

3

说明

OUTPUT DETAILS: There are three ponds: one in the upper left, one in the lower left, and one along the right side.

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 1000005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-4
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ int n, m;
char ch[200][200];
int tot;
int a[200][200];
bool vis[200][200];
int dx[] = { 1,1,1,-1,-1,-1,0,0 };
int dy[] = { 0,1,-1,0,1,-1,1,-1 }; void dfs(int x, int y,int id) {
vis[x][y] = id;
for (int i = 0; i < 8; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (!vis[nx][ny] && a[nx][ny] == 1) {
dfs(nx, ny, id);
}
}
} int main() {
//ios::sync_with_stdio(0);
rdint(n); rdint(m);
for (int i = 1; i <= n; i++)scanf("%s", ch[i] + 1);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (ch[i][j] == 'W')a[i][j] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i][j] == 1 && !vis[i][j]) {
dfs(i, j, ++tot);
}
}
}
cout << tot << endl;
return 0;
}

[USACO10OCT]湖计数Lake Counting 联通块的更多相关文章

  1. 洛谷 P1596 [USACO10OCT]湖计数Lake Counting

    题目链接 https://www.luogu.org/problemnew/show/P1596 题目描述 Due to recent rains, water has pooled in vario ...

  2. 洛谷——P1596 [USACO10OCT]湖计数Lake Counting

    P1596 [USACO10OCT]湖计数Lake Counting 题目描述 Due to recent rains, water has pooled in various places in F ...

  3. P1596 【[USACO10OCT]湖计数Lake Counting】

    可爱的题面君~~ 个人感觉这题还是很简单的,就是一个完全不加工的找联通块个数 个人解题思路是先读入,然后循环一遍,遇到水就dfs,并把这个w所在的联通块“删除”,并在答案上加一 最后输出答案 具体注释 ...

  4. 洛谷P1596 [USACO10OCT]湖计数Lake Counting

    https://www.luogu.org/problemnew/show/P1596 连通块水题... 大体思路是找到是水坑的坐标然后就开始不断递归,往八个方向搜,把连在一起的都标记一遍直到找不到为 ...

  5. Luogu P1596 [USACO10OCT]湖计数Lake Counting

    题目描述 Due to recent rains, water has pooled in various places in Farmer John's field, which is repres ...

  6. $P1596 [USACO10OCT]湖计数Lake Counting$

    \(problem\) 其实这题吧\(DFS\)好写一点(大雾 所以就不讲\(DFS\)了 em \(BFS\)的话 主要是 判重. 方向. 队列.(没了吧 至于位置 用两个队列?还是\(pair\) ...

  7. POJ 2386 Lake Counting(搜索联通块)

    Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 48370 Accepted: 23775 Descr ...

  8. 【HDOJ5713】K个联通块(状压DP,计数)

    题意:有一张无重边的无向图, 求有多少个边集,使得删掉边集里的边后,图里恰好有K个连通块. 1≤T≤201≤K≤N≤140≤M≤N∗(N+1)/21≤a,b≤N 思路:From http://blog ...

  9. 【POJ - 2386】Lake Counting (dfs+染色)

    -->Lake Counting 直接上中文了 Descriptions: 由于近日阴雨连天,约翰的农场中中积水汇聚成一个个不同的池塘,农场可以用 N x M (1 <= N <= ...

随机推荐

  1. Git学习笔记(三)远程库(GitHub)协同开发,fork和忽略特殊文件

    远程库 远程库,通俗的讲就是不再本地的git仓库!他的工作方式和我们本地的一样,但是要使用他就需要先建立连接! 远程库有两种,一个是自己搭建的git服务器:另一种就是使用GitHub,这个网站就是提供 ...

  2. c#指定程序运行指定文件(太好了,终于找到了)

    System.Diagnostics.Process.Start(@"Notepad.exe", "e:\\a.txt"); System.Diagnostic ...

  3. Android Studio Build APK没有报错,但是Generate signed apk报错

    有时候 ,我们在调试APK,直接Build是可以正常生成,没有报错,但是当我们将自己的签名文件加上去,就会报错.一般情况下,我们可以在build.gradle中的android{}里面添加一个东西 l ...

  4. hadoop再次集群搭建(2)-配置免秘钥ssh登录

    SSH对于大多程序员都不陌生,目前主流的云服务提供上也是通过SSH来提供链接的安全保障,比如AWS通过使用下载的私钥(private key)实现与EC2实例安全连接.GitHub通过上传的公钥(pu ...

  5. myeclipse.ini

    myeclipse10 32位 我的配置 #utf8 (do not remove) #utf8 (do not remove) -startup ../Common/plugins/org.ecli ...

  6. DOM详习讲解

    http://www.cnblogs.com/wupeiqi/articles/5643298.html 

  7. 4-4 zk特性 – 理解watcher机制

    watcher是zk里面非常重要的特性.watcher一定要去好好地看一下,一定要去好好地理解一下它是如何去用的,包括触发的事件类型等等.监督者也可以理解为触发器,也就是说当我们的节点发生了一些变化的 ...

  8. 利用powerdesigner创建表模型后导出sql语句方法,以及报错 Generation aborted due to errors detected during the verification of the model.的解决办法

    今天用powerdesigner建了表模型,下面先说一下导出sql语句的步骤. 1.选项 2. 然后就报错了,下面说解决办法,很简单. 你没看错,把模型检查的√去掉就行了~~ 导出表名不带双引号的设置 ...

  9. gitlab 添加ssh秘钥

    在创建新的ssh秘钥对之前,要先确认一下电脑中是否已经有了一对秘钥: Git Bash on Windows / GNU/Linux / macOS / PowerShell: cat ~/.ssh/ ...

  10. sleep()和usleep()

    函数名: sleep头文件: #include <windows.h> // 在VC中使用带上头文件        #include <unistd.h>  // 在gcc编译 ...