[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 ...
随机推荐
- java中获取字母和数字的组合
package com.ccytsoft.wkc.util; import java.util.ArrayList; import java.util.List; import java.util.R ...
- eclipse几种常见问题的解决
build项目时出现卡死现象的解决方案 场景:在使用使用Eclipse编辑文件保存时或者build项目时,经常出现卡死现象,此时即便杀死eclipse进程重启还是依然出现这种现象. 原因:eclips ...
- oracle序列的增、删、改、查及使用
----------------------------------------------------------------------创建序列:示例:CREATE SEQUENCE SEQ_SS ...
- vue脚手架用axios请求本地数据
首先需要声明的是:本地请求,不用考虑跨域问题,这适用刚入坑的前端小白看,小白在做自己的项目时,通常都是用自己写的json数据,之后用axios请求过来,渲染到页面上. 1.cnpm install a ...
- WPF 参数在Page见传递
void goButton_Click(object sender, RoutedEventArgs e) { this.NavigationService.Navigate(new ContentP ...
- 看大师解说Android高速开发框架EasyAndroid
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u010966622/article/details/37601789 前几天做了小应用.感觉小有成就 ...
- Python--共享变量
- 共享变量: 当多个线程同时访问一个变量的时候,会产生共享变量的问题 - 案例11 - 解决变量:锁.信号灯 - 锁(Lock): - 是一个标志,表示一个线程在占用一些资源 - 使用方法 - 上锁 ...
- 客户机容易随机出现自动重启、游戏卡问题?不妨优化下BIOS中节能技术!
相信一些玩主板超频的人都知道,给CPU加电压能够提升CPU的处理速度,很多超频爱好者通过采购超频性能较好,且价格较低的主板和CPU来玩超频,并且通过一些参数的问题能让屌丝设备发挥出高富帅的性能.但是玩 ...
- vue中自定义软键盘
https://segmentfault.com/a/1190000012568480
- Java元注解—— @Retention @Target @Document @Inherited
java中元注解有四个: @Retention @Target @Document @Inherited: @Retention:注解的保留位置 @Retention(RetentionPolicy. ...