[USACO10OCT]湖计数Lake Counting 联通块
题目描述
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.
一行:水坑的数量
输入输出样例
复制
10 12
W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.
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 联通块的更多相关文章
- 洛谷 P1596 [USACO10OCT]湖计数Lake Counting
题目链接 https://www.luogu.org/problemnew/show/P1596 题目描述 Due to recent rains, water has pooled in vario ...
- 洛谷——P1596 [USACO10OCT]湖计数Lake Counting
P1596 [USACO10OCT]湖计数Lake Counting 题目描述 Due to recent rains, water has pooled in various places in F ...
- P1596 【[USACO10OCT]湖计数Lake Counting】
可爱的题面君~~ 个人感觉这题还是很简单的,就是一个完全不加工的找联通块个数 个人解题思路是先读入,然后循环一遍,遇到水就dfs,并把这个w所在的联通块“删除”,并在答案上加一 最后输出答案 具体注释 ...
- 洛谷P1596 [USACO10OCT]湖计数Lake Counting
https://www.luogu.org/problemnew/show/P1596 连通块水题... 大体思路是找到是水坑的坐标然后就开始不断递归,往八个方向搜,把连在一起的都标记一遍直到找不到为 ...
- Luogu P1596 [USACO10OCT]湖计数Lake Counting
题目描述 Due to recent rains, water has pooled in various places in Farmer John's field, which is repres ...
- $P1596 [USACO10OCT]湖计数Lake Counting$
\(problem\) 其实这题吧\(DFS\)好写一点(大雾 所以就不讲\(DFS\)了 em \(BFS\)的话 主要是 判重. 方向. 队列.(没了吧 至于位置 用两个队列?还是\(pair\) ...
- POJ 2386 Lake Counting(搜索联通块)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 48370 Accepted: 23775 Descr ...
- 【HDOJ5713】K个联通块(状压DP,计数)
题意:有一张无重边的无向图, 求有多少个边集,使得删掉边集里的边后,图里恰好有K个连通块. 1≤T≤201≤K≤N≤140≤M≤N∗(N+1)/21≤a,b≤N 思路:From http://blog ...
- 【POJ - 2386】Lake Counting (dfs+染色)
-->Lake Counting 直接上中文了 Descriptions: 由于近日阴雨连天,约翰的农场中中积水汇聚成一个个不同的池塘,农场可以用 N x M (1 <= N <= ...
随机推荐
- 生产者与消费者---demo2---boke
假设有这样一种情况,有一个桌子,桌子上面有一个盘子,盘子里只能放一颗鸡蛋,A专门往盘子里放鸡蛋,如果盘子里有鸡蛋,则一直等到盘子里没鸡蛋,B专门从盘子里拿鸡蛋,如果盘子里没鸡蛋,则等待直到盘子里有鸡蛋 ...
- C++ 学习之---C++基础理论知识(也适合面试回顾)
C++ 语言编写的基础练习 具体案例放在github中 github地址:https://github.com/Master-fd/C-Base 1. 操作符重载 2. 构造与析构 3. 函数模板 4 ...
- 数据库监听。数据库一次notify,Activity多次接收
今天项目中发现一个bug: (1)当uri数据库中有更新,会从数据库层DataService中通知应用层,调用notifyChange: mContext.getContentResolver().n ...
- 免安装Oracle客户端使用PL/SQL连接Oracle
只需要在Oracle下载一个叫Instant Client Package的软件就可以了,这个软件不需要安装,只要解压就可以用了,很方便,就算重装了系统还是可以用的. 下载地址:http://www. ...
- JavaScript的self和this使用小结
一.self这个非常简单.我们知道,打开任何一个网页,浏览器会首先创建一个窗口,这个窗口就是一个window对象,也是js运行所依附的全局环境对象和全局作用域对象.self 指窗口本身,它返回的对象跟 ...
- C++面向对象类的实例题目二
题目描述: 编写一个程序,设计一个产品类Product,其定义如下: class Product { public: Product(char *n,int p,int q); //构造函数 ~Pro ...
- SQLServer+.net 事务锁表问题
最近操作Sqlserver遇到一个锁表问题.找了好久才搞明白原因和解决办法. 故障现象: 每次启动事务后,执行了删除或者修改操作以后,再执行查询操作就锁表. 解决过程: 1:最初以为SQLServer ...
- R: 自动计算代码运行时间
################################################### 问题:代码运行时间 18.4.25 怎么计算代码的运行时间? 解决方案: ptm = pro ...
- bzoj1787 紧急集合
传送门 题目 Input Output 分析 看到这个题不难想到倍增LCA,然后我们考虑如何计算.我们分别求出3个点中任意两点的LCA,为了走的步数最少所以肯定是先有两个点相遇然后另一个点走的它们相遇 ...
- python3-password在输入密码时隐藏密码
# Auther: Aaron Fan #这个脚本请在命令行去执行才可以试出效果,pycharm这里无法测试这个脚本,切记!import getpass _username = "Aaron ...