G - Stealing Harry Potter‘s Precious(bfs+状态压缩

#include<bits/stdc++.h>
using namespace std;
const int N=105;
char ch[N][N];int n,m,k;
map<pair<int,int>,int> mp;
int vis[1<<4][N][N];
struct node{
    int x,y,st;
};
int d1[4]={1,-1,0,0},d2[4]={0,0,1,-1};
int bfs(int x,int y){
    queue<node> q;
    q.push({x,y,0});
    vis[0][x][y]=1;
    while(q.size()){
        node tp=q.front();q.pop();
        if(tp.st==(1<<k)-1) return vis[tp.st][tp.x][tp.y]-1;
        for(int i=0;i<=3;i++){
            int tx=tp.x+d1[i],ty=tp.y+d2[i],nowst=tp.st;
//            cout<<tp.x<<" "<<tp.y<<" ";
//            cout<<tx<<" "<<ty<<endl;
            if(tx<=0||tx>n||ty<=0||ty>m||ch[tx][ty]=='#') continue;
            if(mp[{tx,ty}]){
                nowst=nowst|(1<<((mp[{tx,ty}]-1)));
            }
//            cout<<nowst<<endl;
            if(vis[nowst][tx][ty]) continue;
            vis[nowst][tx][ty]=vis[tp.st][tp.x][tp.y]+1;
            q.push({tx,ty,nowst});
        }
    }
    return -1;
}
int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
            if(n==0&&m==0) break;
        for(int i=1;i<=n;i++) cin>>ch[i]+1;
        scanf("%d",&k);
        mp.clear();memset(vis,0,sizeof vis);
        for(int i=0;i<k;i++){
            int x,y;scanf("%d%d",&x,&y);
            mp[{x,y}]=i+1;
        }
        int st,ed;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(ch[i][j]=='@'){
                    st=i;ed=j;
                    break;
                }
            }
        }
        int ans=bfs(st,ed);
        cout<<ans<<endl;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果要使用Work-Stealing Queue来实现函数一个函数顺序执行,可以将每个函数封装成一个任务,并将这些任务添加到共享的任务队列中。然后,每个线程维护自己的任务队列,并从中取出任务执行,如果自己的队列为空,就从其他线程的队列中“偷”任务执行。 下面是一个使用Work-Stealing Queue实现函数一个函数顺序执行的简单代码示例: ```c++ #include <iostream> #include <thread> #include <vector> #include <deque> #include <atomic> #include <random> std::deque<std::function<void()>> task_queue; std::vector<std::thread> threads; std::atomic<bool> stop_flag{false}; std::vector<std::deque<std::function<void()>>> thread_queues; void worker(int id) { std::default_random_engine engine(id); std::uniform_int_distribution<int> distribution(0, threads.size() - 1); while (!stop_flag) { std::function<void()> task; bool found = false; // 从自己的任务队列中取出任务执行 if (!thread_queues[id].empty()) { task = thread_queues[id].front(); thread_queues[id].pop_front(); found = true; } else { // 从其他线程的任务队列中“偷”任务执行 for (int i = 0; i < threads.size(); ++i) { int index = (id + i) % threads.size(); if (!thread_queues[index].empty()) { task = thread_queues[index].back(); thread_queues[index].pop_back(); found = true; break; } } } // 如果没有找到任务并且任务队列为空,则等待一段时间再继续查找任务 if (!found && task_queue.empty()) { std::this_thread::sleep_for(std::chrono::milliseconds(10)); continue; } // 如果找到了任务,则执行任务 if (found) { task(); } else { // 否则,从任务队列中取出任务并执行 std::lock_guard<std::mutex> lock{task_queue_mutex}; if (!task_queue.empty()) { task = task_queue.front(); task_queue.pop_front(); task(); } } } } void func1() { std::cout << "Function 1" << std::endl; } void func2() { std::cout << "Function 2" << std::endl; } void func3() { std::cout << "Function 3" << std::endl; } int main() { // 创建多个线程 for (int i = 0; i < std::thread::hardware_concurrency(); ++i) { thread_queues.emplace_back(); threads.emplace_back(worker, i); } // 将函数封装成任务并添加到共享的任务队列中 task_queue.emplace_back([]() { func1(); }); task_queue.emplace_back([]() { func2(); }); task_queue.emplace_back([]() { func3(); }); // 等待任务执行完成 while (!task_queue.empty()) { std::this_thread::sleep_for(std::chrono::milliseconds(10)); } // 设置停止标志,等待线程结束 stop_flag = true; for (auto& thread : threads) { thread.join(); } return 0; } ``` 在上面的示例中,我们首先创建多个线程,并将每个线程维护自己的任务队列。然后,我们将函数封装成任务并添加到共享的任务队列中。每个线程循环执行以下操作: 1. 从自己的任务队列中取出任务执行。 2. 如果自己的队列为空,则从其他线程的队列中“偷”任务执行。 3. 如果任务队列为空,则等待一段时间再继续查找任务。 最后,我们等待任务执行完成,设置停止标志,等待线程结束。 这样,我们就可以使用Work-Stealing Queue实现函数一个函数顺序执行的效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值