DFS 之 poj 2386 Lake Counting
// [11/1/2014 JmingS] /* 遍历整个图,找到 'W' 的点,对其周围八个点其进行深搜,若是 'W' 则用 '.' 替换。 最后,在遍历整个图的过程中,找到多少个 'W',即答案。。。 */
1 #include <iostream> 2 #include <cstdlib> 3 #include <cstdio> 4 #include <cmath> 5 #include <algorithm> 6 #include <functional> 7 #include <string> 8 #include <cstring> 9 #include <vector> 10 #include <stack> 11 #include <queue> 12 #include <map> 13 using namespace std; 14 #define eps 1e-8 15 #define MAX 105 16 17 int N, M; 18 char Graph[MAX][MAX]; 19 const int direction[8][2] = { { -1, 1 }, { 0, 1 }, { 1, 1 }, { -1, 0 }, { 1, 0 }, { -1, -1 }, { 0, -1 }, { 1, -1 } }; 20 21 void Dfs(int x, int y) { 22 Graph[x][y] = '.'; 23 for (int i = 0; i < 8; ++i) { 24 int tx = x + direction[i][0], ty = y + direction[i][1]; 25 if ((tx >= 0) && (tx < N) && (ty >= 0) && (ty < M) && ('W' == Graph[tx][ty])) { 26 Dfs(tx, ty); 27 } 28 } 29 return; 30 } 31 32 void Solve() { 33 int Sum = 0; 34 for (int i = 0; i < N; ++i) { 35 for (int j = 0; j < M; ++j) { 36 if ('W' == Graph[i][j]) { 37 Dfs(i, j); 38 //cout << i << " " << j << endl; 39 ++Sum; 40 } 41 } 42 } 43 printf("%d\n", Sum); 44 } 45 46 int main() 47 { 48 //freopen("input.txt", "r", stdin); 49 scanf("%d %d", &N, &M); 50 for (int i = 0; i < N; ++i) { 51 getchar(); 52 for (int j = 0; j < M; ++j) { 53 scanf("%c", &Graph[i][j]); 54 } 55 } 56 Solve(); 57 return 0; 58 }
转载于:https://www.cnblogs.com/shijianming/p/4140795.html
DFS 之 poj 2386 Lake Counting相关推荐
- POJ 2386 Lake Counting [DFS]
POJ 2386 Lake Counting 简单的DFS,用了stack代替递归,输入有问题,搞了蛮久,算法是没问题.所以以后一定要记得加上检查输入的那一步 然后对于点的定义以后就这么办吧 此外还有 ...
- poj 2386 Lake Counting
poj 2386 Lake Counting 题目链接:http://poj.org/problem?id=2386 题目大意:数湖. 题目 ...
- POJ 2386 Lake Counting
链接:http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- POJ 2386 Lake Counting DFS水水
http://poj.org/problem?id=2386 题目大意: 有一个大小为N*M的园子,雨后积起了水.八连通的积水被认为是连接在一起的.请求出院子里共有多少水洼? 思路: 水题~直接DFS ...
- POJ 2386(DFS)
深度优先搜索属于图算法的一种,英文缩写为DFS即Depth First Search.其过程简要来说是对每一个可能的分支路径深入到不能再深入为止,而且每个节点只能访问一次. 举例说明之:下图是一个无向 ...
- Lake Counting POJ - 2386
Due to recent rains, water has pooled in various places in Farmer John's field, which is represented ...
- DFS:深入优先搜索 POJ-2386 Lake Counting
深度优先搜索是从最开始的状态出发,遍历所有可以到达的状态. 因此可以对所有的状态进行操作,或列举出所有的状态. Lake Counting POJ - 2386 Due to recent rains ...
- NUC1158 Lake Counting【DFS】
Lake Counting 时间限制: 1000ms 内存限制: 65536KB 通过次数: 1总提交次数: 1 问题描述 Due to recent rains, water has pooled ...
- G - dfs POJ - 2386
G - dfs POJ - 2386 Due to recent rains, water has pooled in various places in Farmer John's field, w ...
最新文章
- python编写通讯录管理系统_一个简单的python程序实例(通讯录)
- rsync的基本使用
- 一文看懂https如何保证数据传输的安全性的
- 今天开始SOA-阿里dubbo
- python time
- jzoj2292-PPMM【模拟,堆】
- linux-ubuntu下调出中文输入法
- Delphi2CS破解 Delphi 转换C#
- C/C++线程与多线程工作笔记003---C++指针引用和解引用
- Python实现生成100个MAC地址并写入文件中,MAC地址前6位(16进制)为01-AF-3B
- 找出数组中两数之和为指定值的所有整数对
- 英国资深律师称加密货币和智能合约必然将成为社会主流
- Policy Center > Permissions and APIs that Access Sensitive Information
- 蓝鲸cmdb平台架构
- WORD中插入VISIO图形,打印乱码解决
- 通过Transporter迁移MongoDB 数据至elasticsearch
- 一文讲解Linux 内核网络协议栈-数据从接收到ip层
- 神经网络与深度学习笔记 (五)深层神经网络
- 【抽样:理论与应用】期末复习1
- (读书笔记)理性思维看待世界
热门文章
- Struts2核心工作原理解析
- 艾伟_转载:ASP.NET模板引擎技术
- 细节决定成败:一个公共类库
- 学计算机怎么收费呢,学计算机怎么收费服务为先_贵阳经济技术学校
- 如何学好算法与程序设计
- Flume NG之Agent部署和sink配置HDFS且吐槽CSDN博客及客服态度
- 数据结构源码笔记(C语言):哈希表的相关运算算法
- linux lvm 镜像,LVM实现逻辑卷镜像
- python 技术篇-pythoncom.PumpMessag()关闭、杀死它的进程,pythoncom.PumpMessag()运行卡住解决办法
- Windows 技术篇-设置dns提升网速,刷新dns缓存