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 ...
随机推荐
- Javascript 第一阶段 学习使用总结
JavaScript 是一种轻量级的编程语言.JavaScript 是可插入 HTML 页面的编程代码.脚本可被放置在 HTML 页面的 <body> 和 <head> 部分中 ...
- NYOJ-104最大和
我看了好多博客,都是拿一维的做基础,一维的比较简单,所以要把二维的化成一维的,一维的题目大意:给了一个序列,求那个子序列的和最大,这时候就可以用dp来做,首先dp[i]表示第i个数能构成的最大子序列和 ...
- hdu 2211
题意: 中文题目,自己看.............. 递归调用.... 没什么难度,注意下long long就行........ AC代码: #include <iostream> #de ...
- 如何缩减Try{}Catch{}Finally{}代码----定义一个公用的Try{}Catch{}Finally{}
public class Process { public Process() { } public static void Execute(Action action) { try { //ACTI ...
- Android Http请求失败解决方法
1.MainActivity.java 文件中的onCreate方法改成如下: @SuppressLint("NewApi") @Override protected void o ...
- office - 连接字符串.
Microsoft ACE OLEDB 12.0 Connect to Excel 2007 (and later) files with the Xlsx file extension. That ...
- Assembly 'Microsoft.Office.Interop.Excel
编译的时候报错,都无法通过编译: Assembly 'Microsoft.Office.Interop.Excel, Version=14.0.0.0, Culture=neutral, Public ...
- Javascript 常用函数【3】
jquery选中radio //如果之前有选中的,则把选中radio取消掉 $("#tj_cat .pro_category").each(function() { if ($(t ...
- 常用命令常用sql:SHOWVARIABLESLIKE'character%'
mysql学习笔记-常用命令 常用sql: SHOW VARIABLES LIKE 'character%';查看字符集SHOW VARIABLES LIKE 'collation_%';show e ...
- myeclipse插件安装
MyEclipse插件安装 安装1: HELP->MuEclipse Configuration Center.切换到SoftWare选项卡. 点击 add site 打开对话框,在对话框中na ...