POJ No.2386 Lake Counting
题目链接:http://poj.org/problem?id=2386
分析:八联通的则为水洼,我们则需遍历一个单位附近的八个单位并将它们都改成'.',但附近单位可能仍连接着有'W'的区域,这种情况下我们应该用dfs遍历到尽头,并将这些联通的W全部改掉,1此dfs后与初始的这个W连接的所有W都替换成了'.',因此直到图中不再存在W为止,最后执行dfs的次数即为水洼的个数。
#include <iostream>
using namespace std;
int n,m;
char a[][];
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 ;
}
int main(){
int count=;
cin>>n>>m;
for(int i=;i<n;i++){
for(int j=;j<m;j++){
cin>>a[i][j];
}
}
for(int i=;i<n;i++){
for(int j=;j<n;j++){//先从任意一个有dfs的地方开始
if(a[i][j]=='W'){
dfs(i,j);
count++;
}
}
}
cout<<count<<endl;
}
POJ No.2386 Lake Counting的更多相关文章
- POJ 之2386 Lake Counting
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20003 Accepted: 10063 D ...
- POJ 2386 Lake Counting(深搜)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17917 Accepted: 906 ...
- POJ 2386 Lake Counting
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28966 Accepted: 14505 D ...
- [POJ 2386] Lake Counting(DFS)
Lake Counting Description Due to recent rains, water has pooled in various places in Farmer John's f ...
- POJ 2386 Lake Counting(搜索联通块)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 48370 Accepted: 23775 Descr ...
- POJ:2386 Lake Counting(dfs)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40370 Accepted: 20015 D ...
- poj 2386:Lake Counting(简单DFS深搜)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18201 Accepted: 9192 De ...
- POJ 2386 Lake Counting 八方向棋盘搜索
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 53301 Accepted: 26062 D ...
- poj - 2386 Lake Counting && hdoj -1241Oil Deposits (简单dfs)
http://poj.org/problem?id=2386 http://acm.hdu.edu.cn/showproblem.php?pid=1241 求有多少个连通子图.复杂度都是O(n*m). ...
随机推荐
- Spring整合Redis&JSON序列化&Spring/Web项目部署相关
几种JSON框架用法和效率对比: https://blog.csdn.net/sisyphus_z/article/details/53333925 https://blog.csdn.net/wei ...
- hash进阶:使用字符串hash乱搞的姿势
前言 此文主要介绍hash的各种乱搞方法,hash入门请参照我之前这篇文章 不好意思hash真的可以为所欲为 在开头先放一下题表(其实就是我题解中的hash题目qwq) 查询子串hash值 必备的入门 ...
- 【ASP.NET】System.Web.Routing - Route Class
Provides properties and methods for defining a route and for obtaining information about the route. ...
- Git stash 常用命令
参考: Git: How to look at the stash Git学习笔记05--git stash Git stash 常用命令 1.git stash: 保存当前的工作进度: 2.git ...
- TCP/UDP协议、理解三次握手四次挥手、Socket
一.什么是socket? 中文名叫套接字,是对底层的 TCP IP UDP 等网络协议进行封装,使得上层的应用程序开发者,不用直接接触这对复杂,丑陋的协议. 在程序员的言论,他就是一个封装好的模块,要 ...
- 【NOI 2016】优秀的拆分
Problem Description 如果一个字符串可以被拆分为 \(AABB\) 的形式,其中 \(A\) 和 \(B\) 是任意非空字符串,则我们称该字符串的这种拆分是优秀的. 例如,对于字符串 ...
- Python: 字典应用题
Write a program to read through the mbox-short.txt and figure out who has sent the greatest number o ...
- MVC ---- 怎删改查
using Modelsop; using System; using System.Collections.Generic; using System.Linq; using System.Web; ...
- javascript 创建video元素
<!DOCTYPE html> <html> <body> <h3>演示如何创建 VIDEO 元素</h3> <p>请点击按钮来 ...
- Xshell5中常用linux服务器命令集合
简易版:http://www.zhimengzhe.com/linux/84546.html 详细版:http://www.cnblogs.com/peida/tag/%E6%AF%8F%E6%97% ...