挑战程序设计竞赛:Lake Counting

题目大意

在这里插入图片描述

解题思路

  • 遇到一个Wans++并且利用深搜将该联通区域的所有W去掉。

代码

#include<iostream>
using namespace std;
const int MAX = 105;

char grad[MAX][MAX];
int ans;
int N, M;
bool out_of_edge(int x, int y)
{
    if(x < 0 || x >= N || y < 0 || y >= M)
        return true;
    return false;
}
int dir[8][2] = {{1,0},{-1,0},{0,1},{0,-1},{1,1},{-1,-1},{-1,1},{1,-1}};
void dfs(int x, int y)
{
    grad[x][y] = '.';
    for(int i=0; i<8; i++)
    {
        int nx = x + dir[i][0];
        int ny = y + dir[i][1];
        if(out_of_edge(nx,  ny))
            continue;
        if(grad[nx][ny] == '.')
            continue;
        dfs(nx, ny);
    }
    return;
}
int main()
{
    cin >> N >> M;
    for(int i=0; i<N; i++)
        for(int j=0; j<M; j++)
            cin >> grad[i][j];
    ans = 0;
    for(int i=0; i<N; i++)
    {
        for(int j=0; j<M; j++)
        {
            if(grad[i][j] == 'W')
            {
                ans++;
                dfs(i, j);
            }
        }
    }
    cout << ans << endl;
    return 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、付费专栏及课程。

余额充值