[openjudge-搜索]Lake Counting(翻译及实现)
题目原文
描述
Due to recent rains, water has pooled in various places in Farmer John’s field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water (‘W’) or dry land (‘.’). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors.
Given a diagram of Farmer John’s field, determine how many ponds he has.
输入
* Line 1: Two space-separated integers: N and M
- 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.
输出 - Line 1: The number of ponds in Farmer John’s field.
样例输入
10 12
W……..WW.
.WWW…..WWW
….WW…WW.
………WW.
………W..
..W……W..
.W.W…..WW.
W.W.W…..W.
.W.W……W.
..W…….W.
样例输出
3
提示
OUTPUT DETAILS:
There are three ponds: one in the upper left, one in the lower left,and one along the right side.
题目翻译
描述
由于最近下雨,水汇集在农民约翰的领地各处,这是由一个矩形的N x M(1 < = N < = 100;1 < = M < = 100)矩阵。每个矩阵包含水(’ W ‘)或陆地(’ . ‘)。农民约翰想算出有多少池塘形成在他的领域。与一个水池再8个方向连接的被看作是一个池塘。
给定一个农民约翰领地的地图,求出有多少池塘。
输入
第一行:两个空格分隔的整数:N和M
第二行到N + 1行:M每行字符代表一行的农民约翰的领域。每个字符’ W ‘或’。’。字符与字符之间没有空格。
输出
第1行:池塘的数量
实现
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int head=0,tail=1,q,nextx,nexty,n,m,startx,starty,cont;
int a[100005],b[100005],x[8]={0,1,1,1,0,-1,-1,-1},y[8]={1,1,0,-1,-1,-1,0,1},c;
char map[105][105];
bool t[105][105];
bool chek(int qx,int qy)
{
if(qx<=n-1&&qy<=m-1&&qx>=0&&qy>=0)return 1;
return 0;
}
void dfs()
{
memset(b,0,sizeof(b));
memset(a,0,sizeof(a));
a[1]=startx;
b[1]=starty;
t[startx][starty]=1;
head=0;tail=1;
while(head!=tail)
{
head++;
for(int i=0;i<=7;i++)
{
nextx=a[head]+x[i];
nexty=b[head]+y[i];
if(!t[nextx][nexty]&&map[nextx][nexty]=='W'&&chek(nextx,nexty))
{
t[nextx][nexty]=1;
tail++;
map[nextx][nexty]='#';
a[tail]=nextx;
b[tail]=nexty;
}
}
}
}
int main()
{
c=0;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
scanf("%s",map[i]);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(map[i][j]=='W')
{startx=i;starty=j;map[i][j]='#';cont++;dfs();}
printf("%d\n",cont);
}
[openjudge-搜索]Lake Counting(翻译及实现)的更多相关文章
- 深度搜索---------Lake counting
#include<iostream>#include<cstdio>#include<cstdlib>#define maxn 100char ch[maxn][m ...
- POJ 2386 Lake Counting(搜索联通块)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 48370 Accepted: 23775 Descr ...
- POJ 2386 Lake Counting 八方向棋盘搜索
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 53301 Accepted: 26062 D ...
- Poj2386 Lake Counting (DFS)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 49414 Accepted: 24273 D ...
- poj 2386:Lake Counting(简单DFS深搜)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18201 Accepted: 9192 De ...
- Openjudge1388 Lake Counting【DFS/Flood Fill】
http://blog.csdn.net/c20182030/article/details/52327948 1388:Lake Counting 总时间限制: 1000ms 内存限制: ...
- 【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 ...
随机推荐
- 20165225《Java程序设计》第九周学习总结
20165225<Java程序设计>第九周学习总结 1.视频与课本中的学习: 第十三章学习总结 URL类 URL对象包含三部分信息:协议.地址和资源 创建URL对象两种方法: public ...
- 函数datetime
datetime import datetime.datetime print(datetime.now()) #2019-03-20 11:35:25.(471359)毫秒 # 时间对象 f = d ...
- 重读《深入理解Java虚拟机》四、虚拟机如何加载Class文件
1.Java语言的特性 Java代码经过编译器编译成Class文件(字节码)后,就需要虚拟机将其加载到内存里面执行字节码所定义的代码实现程序开发设定的功能. Java语言中类型的加载.连接(验证.准备 ...
- int(1)和int(11)是否有区别?
MySQL类型关键字后面的括号内指定整数值的显示宽度(例如,INT(11)).该可选显示宽度规定用于显示宽度小于指定的列宽度的值时从左侧填满宽度.显示宽度并不限制可以在列内保存的值的范围,也不限制超过 ...
- The iOS Simulator deployment target is set to 6.0
XCODE警告 Showing All Messages :-1: The iOS Simulator deployment target is set to 6.0, but the range o ...
- 20170927 Webservice发布指定账户进行访问
1. 搭建IIS 平台 于服务器A1 2.发布Webservice 到A1 我的问题在于(Webservice中方法中内容会对B1服务器的共享路径进行写入文件动作), 如何来控制网页使用特定的账户去访 ...
- [py][mx]django的cookie和session操作-7天免登录
浏览器同源策略(same-origin policy) csrf攻击防御核心点总结 django的cookie和session操作-7天免登录 flask操作cookie&django的see ...
- 第二弹:超全Python学习资源整理(进阶系列)
造一个草原要一株三叶草加一只蜜蜂.一株三叶草,一只蜂,再加一个梦.要是蜜蜂少,光靠梦也行. - 狄金森 "成为编程大牛要一门好语言加一点点天分.一门好语言,一点点天分,再加一份坚持.要是天分 ...
- crontab-rsync
写一个shell脚本放到crontab中,该脚本利用rsync把远程同步到本地的话,需要把本地的公钥放到远程的authorized_keys:否则,手动执行脚本没问题,但是crontab执行就不会有效 ...
- 新手详解JAVA+数据库+JSP完成简单页面
本篇以数据库添加为例(本例中数据库名为“xinxi”表单名字为“stud”) 准备---实体层: package entity; public class Student { private Stri ...