POJ2386——经典的DFS
来源:互联网 发布:h3c 查看端口聚合 编辑:程序博客网 时间:2024/06/06 04:14
了解DFS算法,不妨看看 http://blog.csdn.net/chuck001002004/article/details/50396329 详解!
http://poj.org/problem?id=2386
题意:n*m的院子,‘ W ’代表有水,‘ . ’代表无水,若一个格子的八个方向存在‘ W ’则可以看做一个水塘,求一共有几个水塘
分析:经典的DFS,从第一个点开始遍历,只要发现其为‘ W ’便将他变为‘ . ’,之后遍历它的八个方向,如果发现有‘ W ’存在则重复上述操作。
#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#include <math.h>using namespace std;char a[105][105];int n,m,sum;void DFS(int i,int j){ a[i][j]='.'; //千万将他变为'.',不然无法跳出递归 for(int x=-1;x<=1;x++) //遍历它的八个方向 { for(int y=-1;y<=1;y++) { //满足在院子范围内且对应的值为'W' if(a[i+x][j+y]=='W'&&(i+x)>0&&(i+x)<=n&&(j+y)>0&&(j+y)<=m) DFS(i+x,j+y); //遍历它的八个方向 } } return ;}int main(){ while(cin>>n>>m) { sum=0; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>a[i][j]; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(a[i][j]=='W') { DFS(i,j); sum++; //这两步千万不要反了,否则就会是统计‘W’的个数 } } } cout<<sum<<endl; } return 0;}
0 0
- POJ2386——经典的DFS
- POJ2386经典DFS深搜
- 【DFS】(一)最简单的递归dfs——水坑问题(poj2386)
- poj2386-dfs
- poj2386 DFS
- UVA 572&& PoJ2386 两道DFS入门最经典的题目
- POJ2386:Lake Counting(DFS)
- poj2386 Lake Counting DFS
- 简单dfs--poj2386
- POJ2386 Lake Counting(DFS)
- POJ2386 Lake Counting 【DFS】
- poj2386(dfs)
- poj2386水洼dfs
- poj2386 (DFS)
- POJ2386-简单BFS/DFS
- POJ2386 Lake Counting (dfs)
- POJ2386 Lake Counting(dfs)
- poj2386 Lake Counting【DFS】
- leetcode -- Range Sum Query - Mutable -- 重点,有线段树
- 网站后台登录地址大全
- 千载难逢的大数据技术学习机会
- 【Linux 驱动】Netfilter/Iptables (七) 内核协议栈skb封装分析(续六)
- iOS 的VFL知识详解
- POJ2386——经典的DFS
- 设置文本框样式和修改按钮样式
- [BZOJ3675] [Apio2014]序列分割
- 使用AfxBeginThread须注意的问题...
- 死锁演示 Lock
- web设置session超时时间
- 互联网创业的感想
- 请求参数里面有汉字的处理
- tomcat部署上之后,运行报错