Lake Counting--poj2386
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 23950 | Accepted: 12099 |
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 这个题可以用深搜也可以用广搜,我就是从这个题,明白了两种搜索方式的不同 大家来体会一下吧! DFS版:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; char map[][];
int mov[][]={,,,-,-,,,,,,-,-,,-,-,};
int m,n;
bool can(int x ,int y)//判断这个点能不能走
{
if(x<||x>m-||y<||y>n-||map[x][y]=='.')
return false;
return true;
} void dfs(int x,int y)
{
int i,xx,yy;
for(i=;i<;i++)
{
xx=x+mov[i][];
yy=y+mov[i][];
if(can(xx,yy))
{
map[xx][yy]='.';//走一个点就标记一下
dfs(xx,yy);
}
}
}
int main()
{
int i,j;
while(scanf("%d%d",&m,&n)!=EOF)
{
int sum=;
for(i=;i<m;i++)
scanf("%s",map[i]);
for(i=;i<m;i++)
{
for(j=;j<n;j++)
{
if(map[i][j]=='W')
{
map[i][j]='.';
dfs(i,j);//每次进入搜索函数就把这个点周围能走的点走完
sum++;
}
}
}
printf("%d\n",sum);
}
return ;
}
BFS版:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
char map[][];
int m,n;
int mov[][]={,,,-,,,-,,,,-,-,,-,-,};
struct node
{
int a,b;
}ta,tb;//定义一个结构体用来存坐标
bool can(node x)
{
if(x.a<||x.a>m-||x.b<||x.b>n-||map[x.a][x.b]=='.')
return false;
return true;
} void bfs(int x,int y)
{
queue<node> q;
ta.a=x;
ta.b=y;
q.push(ta);//入队
while(!q.empty())//直到把队列能访问的都访问过
{
int i;
ta=q.front();
q.pop();
for(i=;i<;i++)
{
tb.a=ta.a+mov[i][];
tb.b=ta.b+mov[i][];
if(can(tb))
{
map[tb.a][tb.b]='.';
q.push(tb);//如果可以访问就入队
}
}
}
} int main()
{
int i,j;
while(scanf("%d%d",&m,&n)!=EOF)
{
int sum=;
for(i=;i<m;i++)
scanf("%s",map[i]);
for(i=;i<m;i++)
for(j=;j<n;j++)
{
if(map[i][j]=='W')
{
map[i][j]='.';
bfs(i,j);
sum++;
}
}
printf("%d\n",sum);
}
return ;
}
Lake Counting--poj2386的更多相关文章
- Poj2386 Lake Counting (DFS)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 49414 Accepted: 24273 D ...
- POJ2386 Lake Counting 【DFS】
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20782 Accepted: 10473 D ...
- 【POJ - 2386】Lake Counting (dfs+染色)
-->Lake Counting 直接上中文了 Descriptions: 由于近日阴雨连天,约翰的农场中中积水汇聚成一个个不同的池塘,农场可以用 N x M (1 <= N <= ...
- POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)
来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS Memory Limit: 65536 ...
- 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 ...
- bzoj1751 [Usaco2005 qua]Lake Counting
1751: [Usaco2005 qua]Lake Counting Time Limit: 5 Sec Memory Limit: 64 MB Submit: 168 Solved: 130 [ ...
- BZOJ 3385: [Usaco2004 Nov]Lake Counting 数池塘
题目 3385: [Usaco2004 Nov]Lake Counting 数池塘 Time Limit: 1 Sec Memory Limit: 128 MB Description 农夫 ...
- 3385: [Usaco2004 Nov]Lake Counting 数池塘
3385: [Usaco2004 Nov]Lake Counting 数池塘 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 22 Solved: 21 ...
- 1751: [Usaco2005 qua]Lake Counting
1751: [Usaco2005 qua]Lake Counting Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 190 Solved: 150[Su ...
随机推荐
- mysql 性能分析套件
#!/usr/local/python3./bin/python3. #!coding:utf- #################################### #目地:用于诊断mysql性 ...
- tomcat 项目部署问题
我本地Tomcat版本:Apache Tomcat/8.0.3.0 服务器端:Apache Tomcat/6.0.37 JVM都是:1.7.0_40-b43 之前项目运行正常,在我更新了一些模块后,重 ...
- 关于ArcGIS Rest API
ArcGIS Rest API: 9.3版本: http://resources.esri.com/help/9.3/arcgisserver/apis/rest/index.html 10版本:ht ...
- django项目环境搭建备忘
由于使用python3,所以尽量为每个项目配置虚拟环境来管理各个项目的=. 新建一个项目文件夹,进入该路径 python3 -m venv ll_env 然后激活虚拟环境 source ll_env/ ...
- JavaScript-打开新窗口
open()方法可以查找一个已经存在或者新建一个新的浏览器窗口. 语法:window.open([URL], [窗口名称], [参数字符串]) 参数解释: URL:可选参数,在窗口中显示网页的网址或路 ...
- 如何对应用服务性能问题诊断(Tomcat、Weblogic中间件)
在我们web项目中,我们常见的web应用服务器有Tomcat.Weblogic.WebSphere.它们是互联网应用系统的基础架构软件,也叫“中间件”,负责处理动态在页面请求,并为应用提供了名字.事务 ...
- SSCTF Final PWN
比赛过去了两个月了,抽出时间,将当时的PWN给总结一下. 和线上塞的题的背景一样,只不过洞不一样了.Checksec一样,发现各种防护措施都开了. 程序模拟了简单的堆的管理,以及cookie的保护机制 ...
- .NETFramework类库
.NET Framework 包括可加快和优化开发过程并提供对系统功能的访问的类.接口和值类型. 为了便于语言之间进行交互操作,大多数 .NET Framework 类型都符合 CLS,因而可在编译器 ...
- JavaScript面向对象精要
来自:淡忘~浅思. 链接:http://www.ido321.com/1585.html 和 http://www.ido321.com/1586.html 数据类型 在JavaScript中,数 ...
- MVC 授权过滤器 AuthorizeAttribute
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...