POJ-2386(深广搜基础)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 25322 | Accepted: 12759 |
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
思路:
很基础的一道题目,入门级别,有助于理解搜索的本质以及bfs和dfs的区别
这个题可以用“扫雷”的思维来形象的理解,即遇到一个'W',点一下这个点,则与它相邻的一片为‘W’的点都变成‘.’了
而我们只需要从头开始遍历一共有多少个这样的W即可
dfs:
#include <stdio.h>
#define maxn 107 int n,m;
char g[maxn][maxn];
int dir[][] = {{-,-},{-,},{-,},{,-},{,},{,},{,-},{,}}; void dfs(int x,int y){
g[x][y] = '.';//此行代码意义重大,相当于将其置为已访问状态
for(int i = ;i < ;i++) {
int dx = x+dir[i][];
int dy = y+dir[i][];
if(dx>n||dx<||dy<||dy>m)
continue;
if(g[dx][dy] == '.')
continue;
dfs(dx,dy);
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF) {
int ans = ;
for(int i = ;i <= n;i++)
scanf("%s",g[i]+);
int cnt = ;
for(int i = ;i <= n;i++)
for(int j = ;j <= m;j++) {
if(g[i][j] == 'W') {
ans++;
dfs(i,j);//把所有和该点相邻的W都变成.
}
}
printf("%d\n",ans);
}
return ;
}
bfs:
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std; int n,m;
char G[][];
int dir[][] = {{-,-},{-,},{-,},{,-},{,},{,-},{,},{,}};
typedef pair<int,int> node; void bfs(int x,int y)
{
G[x][y] ='.';//比较好的一种处理方法,省去开vis数组
priority_queue<node> q;//q中存储了(x,y)点的所有连通点
q.push(make_pair(x,y));
while(!q.empty())
{
node t = q.top();
q.pop();
int tx = t.first;
int ty = t.second;
for(int i = ;i < ;i++)
{
int dx = tx+dir[i][];
int dy = ty+dir[i][];
if(dx>=&&dx<=n&&dy>=&&dy<=m && G[dx][dy]=='W') {
G[dx][dy] = '.';
q.push(make_pair(dx,dy));
}
}
}
} int main()
{
while(cin>>n>>m)
{
int ans = ;
for(int i = ;i <= n;i++)
scanf("%s",&G[i][]); for(int i = ;i <= n;i++)
for(int j = ;j <= m;j++)
if(G[i][j]=='W') {
bfs(i,j);
ans++;
}
cout<<ans<<endl;
}
return ;
}
POJ-2386(深广搜基础)的更多相关文章
- POJ 2386 Lake Counting (简单深搜)
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 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: 17917 Accepted: 906 ...
- POJ 2386 DFS深搜入门
题目链接 Time Limit: 1000MS Memory Limit: 65536K Description Due to recent rains, water has pooled in va ...
- poj和hdu部分基础算法分类及难度排序
最近想从头开始刷点基础些的题,正好有个网站有关于各大oj的题目分类(http://www.pythontip.com/acm/problemCategory),所以写了点脚本把hdu和poj的一些题目 ...
- POJ 1190(深搜)
http://poj.org/problem?id=1190 又有好久没做搜索的题了,没想到做一个卡了我那么久,想哭啊. 一个中文题,思路呢也就是搜索呗,一层一层往上面搜,不过这里有两个比较重要的地方 ...
- POJ 2386
http://poj.org/problem?id=2386 这个题目与那个POJ 1562几乎是差不多的,只不过那个比这个输入要复杂一些 #include <stdio.h> #incl ...
- 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). ...
- DFS----Lake Counting (poj 2386)
Lake Counting(POJ No.2386) Description Due to recent rains, water has pooled in various places in Fa ...
随机推荐
- java JMS消息队列
http://blog.csdn.net/shirdrn/article/details/6362792 http://haohaoxuexi.iteye.com/blog/1893038 http: ...
- Java基础知识强化89:Date类之Data类概述及其方法
1. Date类概述 类Date表示特定的瞬间,精确到毫秒 2. 构造方法 public Date():根据当前默认毫秒值创建日期对象 public Date(long date):根据给定的毫秒值创 ...
- git常用命令<转>
(转自)https://www.akii.org/git-concise-operating-tutorial.html git工作原理: 分布式,每个克隆或更新远程仓库的用户都拥有⼀一份最新的完整的 ...
- 线段树---HDU1166敌兵布阵
这个是线段树中最入门的题目,但是由于不了解线段树的概念,当然更不知道怎么样,所以觉得挺费劲,整了一会发现还是基本的思想,就是还是将一个线段继续分割,一直分割到不能分割,这道题目是知道多少个军营,也就是 ...
- SQL读取系统时间的语法(转)
--获取当前日期(如:yyyymmdd) select CONVERT (nvarchar(12),GETDATE(),112) --获取当前日期(如:yyyymmdd hh:MM:ss)select ...
- SuperSocket快速入门(一):什么是SuperSocket
什么是SuperSocket SuperSocket(下文简称SS)是一个轻量级, 跨平台而且可扩展的 .Net/Mono Socket 服务器程序框架.你无须了解如何使用 Socket, 如何维护 ...
- 各版本IIS安装方法
各版本IIS安装方法 Windows 2000 V5.0 将操作系统安装光盘放入光驱,打开“控制面板”→“添加或删除程序”→“添加/删除 Windows 组件”,勾选“Internet信息服务(I ...
- MySQL教程:数据库具体操作
1. 连接数据库服务器 $ ./mysql -h host_name -u user_name -p -h host_name(--host=host_name),连接的数据库主机名,如果在本地主机上 ...
- C# 导出word文档及批量导出word文档(3)
在初始化WordHelper时,要获取模板的相对路径.获取文档的相对路径多个地方要用到,比如批量导出时要先保存文件到指定路径下,再压缩打包下载,所以专门写了个关于获取文档的相对路径的类. #regio ...
- JavaScript--Json对象
JSON(JavaScript Object Notation)一种简单的数据格式,比xml更轻巧.JSON是JavaScript原生格式,这意味着在JavaScript中处理JSON数据不需要任何 ...