Poj2386 Lake Counting (DFS)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 49414 | Accepted: 24273 |
Description
Given a diagram of Farmer John's field, determine how many ponds he has.
Input
* 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
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
There are three ponds: one in the upper left, one in the lower left,and one along the right side.
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
char a[][];
int n,m;
void dfs(int x,int y)
{
a[x][y]='.';
for(int dx=-;dx<=;dx++){
for(int dy=-;dy<=;dy++){
int nx=x+dx,ny=y+dy;
if(nx>=&&nx<n&&ny>=&&ny<m&&a[nx][ny]=='W'){
dfs(nx,ny);
}
}
}
return ;
}
void solve()
{
int res=;
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(a[i][j]=='W'){
dfs(i,j);
res++;
}
}
}
cout<<res<<endl;
}
int main()
{
while(cin>>n>>m){
for(int i=;i<n;i++){
for(int j=;j<m;j++){
cin>>a[i][j];
}
}
solve();
}
return ;
}
自己再熟悉一遍:
#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
int n,m;
char a[][];
int res;
int nx,ny;
void dfs(int x,int y)
{
//八个方向搜索
for(int i=-;i<=;i++){
for(int j=-;j<=;j++){
nx=x+i,ny=y+j;
if(nx>=&&nx<n&&ny>=&&ny<m&&a[nx][ny]=='W'){
a[nx][ny]='.';
dfs(nx,ny);
}
}
}
}
void solve()
{
res=;
//每次从发现W的时候开始搜索由于一簇只能算一个地方所以res计数只加一次
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(a[i][j]=='W'){
res++;
dfs(i,j);
}
}
}
}
int main()
{
while(cin>>n>>m){
memset(a,,sizeof(a));
for(int i=;i<n;i++){
for(int j=;j<m;j++){
cin>>a[i][j];
}
}
solve();
cout<<res<<endl;
}
return ;
}
Poj2386 Lake Counting (DFS)的更多相关文章
- POJ:2386 Lake Counting(dfs)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40370 Accepted: 20015 D ...
- POJ 2386——Lake Counting(DFS)
链接:http://poj.org/problem?id=2386 题解 #include<cstdio> #include<stack> using namespace st ...
- [USACO10OCT]Lake Counting(DFS)
很水的DFS. 为什么放上来主要是为了让自己的博客有一道DFS题解,,, #include<bits/stdc++.h> using namespace std; ][],ans,flag ...
- Lake Counting(dfs)
Description Due to recent rains, water has pooled in various places in Farmer John's field, which is ...
- 【POJ - 2386】Lake Counting (dfs+染色)
-->Lake Counting 直接上中文了 Descriptions: 由于近日阴雨连天,约翰的农场中中积水汇聚成一个个不同的池塘,农场可以用 N x M (1 <= N <= ...
- POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)
来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS Memory Limit: 65536 ...
- poj-2386 lake counting(搜索题)
Time limit1000 ms Memory limit65536 kB Due to recent rains, water has pooled in various places in Fa ...
- POJ2386 Lake Counting 【DFS】
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20782 Accepted: 10473 D ...
- poj2386 Lake Counting(简单DFS)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj ...
随机推荐
- 初学UML之-------用例图
本文转载至:http://blog.csdn.net/a649518776/article/details/7493148 一.UML简介 UML(统一建模语言,Unified Modeling L ...
- 机器学习 —— 深度学习 —— 基于DAGNN的MNIST NET
DAGNN 是Directed acyclic graph neural network 缩写,也就有向图非循环神经网络.我使用的是由MatConvNet 提供的DAGNN API.选择这套API作为 ...
- AbstractQueuedSynchronizer-AQS
AbstractQueuedSynchronizer,这个类是整个java.util.concurrent的核心之一. CountDownLatch 能够使一个或多个线程等待其他线程完成各自的工作后再 ...
- asp.net mvc 通过StyleBundle添加样式后,没有作用
在App_Start/BundleConfig配置 导入bootstrap,但不起作用,代码如下: bundles.Add(new StyleBundle("~/Content/bootst ...
- ubuntu开机启动
/rc.local (ran as root) https://unix.stackexchange.com/questions/210939/what-user-runs-the-commands ...
- PHP版本MS17-010检测小脚本
内网渗透的时候有点用处,可以检测MS17-010的漏洞并获取操作系统信息,配合BURP可批量检测,纯socket发包,无需其他扩展. <?php //根据巡风python代码翻译成PHP代码 / ...
- Oracle课程档案,第十天
用户管理 Authentication: 身份验证 AAA:Authentication: 身份验证 Authorization: 权限管理 Audition: 审计 grant:授权 unset:撤 ...
- 分布式事务TransactionScope
分布式事务TransactionScope 以下是分布式事务的所有情况的例子了,包含了事务套事务,事务套存储过程事务,经过测试,TransactionScope对于分布式事务的各种情况支持的很好. 使 ...
- Android100【申明:来源于网络】
Android100[申明:来源于网络] 地址:http://www.android100.org/html/201406/11/23770.html
- CentOS7.6 安装Docker
删除已安装的Docker # Uninstall installed docker sudo yum remove docker \ docker-client \ docker-client-lat ...