openjudge_2.5基本算法之搜索_1388:Lake Counting

这篇文章介绍了一个编程问题,要求在给定的矩形田地中,通过宽度搜索算法计算形成的所有相连水区域(即池塘)的数量。输入是田地的矩阵,输出是池塘的数量。
摘要由CSDN通过智能技术生成

题目

1388:Lake Counting
查看提交统计提问
总时间限制: 1000ms 内存限制: 65536kB
描述
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

翻译

题目
由于due最近的降雨,农民约翰的田地field里的各个various 地方都积水pooled 了,
用N x M(1<=N<=100;1<=M<=100)正方形squares的矩形rectangle 表示represented 。
每个正方形包含水(“W”)或旱地(“.”)。
农夫约翰想弄清楚figure 他的田里形成了多少池塘ponds 。池塘是一组相连connected 的正方形,其中有水,
其中一个正方形被认为considered 与它的所有八个邻居相邻adjacent 。
给出农夫约翰田地的示意图diagram ,确定determine 他有多少池塘。

输入
*第1行:两个空格分隔的整数:N和M
*第2…N+1:M个字符每行代表representing 农民约翰的田地的一行。每个字符都是“W”或“.”。
字符之间没有空格。

输出
*第1行:农夫约翰田地里的池塘数量。

理解

精力都花在翻译上,没啥就是宽搜。
'W’就算作一个池塘,而且周围8个点中的’W’都算入该池塘。
看下整个field有几个ponds。

代码

#include <bits/stdc++.h>
using namespace std;
struct squares{
int x,y;
};
int n,//行
m,//列
ans,
d[8][2]={{0,-1},{-1,-1},{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1}};
char field[110][110];
bool k[110][110];
void view(){
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++)cout<<field[i][j]<<" ";
cout<<endl;
}
}
bool ok(int x,int y){
return x>=1&&x<=n&&y>=1&&y<=m&&!k[x][y]&&field[x][y]= =‘W’;
}
int main(){
//freopen(“data.cpp”,“r”,stdin);
cin>>n>>m;
cin.get();//撇清换行符号
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
field[i][j]=cin.get();
}
cin.get();
}
//view();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)//遍历每块土地
if(field[i][j]==‘W’&&!k[i][j]){//如果是水,而且还没算成哪块水塘
k[i][j]=1;ans++;//以该块土地为一个池塘,
queue q;q.push(squares{i,j});//队列
while(!q.empty()){//队列非空就宽搜
squares s=q.front();q.pop();//取得对首元素
for(int o=0;o<8;o++){//遍历8个方向
int x=s.x+d[o][0],y=s.y+d[o][1];
if(ok(x,y)){//如果是水,并且没算过,都算入该池塘
k[x][y]=1;q.push(squares{x,y});
}
}
}
}
cout<<ans;
return 0;
}

小结

提高阅读理解能力

  • 23
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`std::counting_semaphore` 是 C++20 新增的同步原语之一,用于控制多个线程间的访问。它是一个计数信号量,可以用来限制同时访问某个资源的线程数量。在类的成员中使用 `std::counting_semaphore` 与在其他地方使用它并没有本质的区别,只需要在类的定义中声明一个 `std::counting_semaphore` 类型的成员即可。 以下是一个简单的示例代码: ```c++ #include <semaphore> #include <thread> #include <iostream> class Example { public: Example() : sema_(2) {} void do_something() { sema_.acquire(); std::cout << "Doing something..." << std::endl; std::this_thread::sleep_for(std::chrono::seconds(1)); sema_.release(); } private: std::counting_semaphore<2> sema_; }; int main() { Example e; std::thread t1(&Example::do_something, &e); std::thread t2(&Example::do_something, &e); std::thread t3(&Example::do_something, &e); t1.join(); t2.join(); t3.join(); return 0; } ``` 在这个例子中,`Example` 类中定义了一个 `std::counting_semaphore<2>` 类型的成员 `sema_`,用于控制同时访问 `do_something` 函数的线程数量。在 `do_something` 函数中,线程首先需要调用 `acquire()` 函数获取信号量,如果当前已经有两个线程在访问,则该线程会被阻塞,直到有一个线程调用了 `release()` 函数释放了信号量。在主函数中,我们创建了三个线程来同时访问 `do_something` 函数,由于信号量的数量是 2,因此最多只有两个线程能够同时访问,第三个线程需要等待前面的线程释放信号量后才能继续执行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值