zoukankan      html  css  js  c++  java
  • newcode wyh的吃鸡(优势队列+BFS)题解

    思路:

    要用优势队列,因为有的+2,有的+1,所以队列中的步长是不单调的,所以找到一个答案但不一定最小,所以用优势队列把小的放在队首。

    要记录状态,所以开了三维,题目和昨天做的那道小明差不多

    vis开的int型赋值bool型WA了半天

    代码:

    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<queue>
    #define ll long long
    using namespace std;
    const int N = 100+5;
    char mp[N][N];
    int vis[3][N][N],k,x,y,n,to[4][2] = {0,1,0,-1,1,0,-1,0};
    struct node{
        int x,y;
        int step,speed;
        bool operator < (const node &a) const{
            return step > a.step;
        }
    };
    void BFS(){
        priority_queue<node> q;
        while(!q.empty()) q.pop();
        node a,b;
        a.x = x,a.y = y,a.speed = 2,a.step = 0;
        vis[a.speed][a.x][a.y] = 1;
        q.push(a);
        while(!q.empty()){
            a = q.top();
            q.pop();
            if(mp[a.x][a.y] == 'X' && a.step <= k){
                printf("YES
    %d
    ",a.step);
                return;
            }
            for(int i = 0;i < 4;i++){
                b.x = a.x + to[i][0];
                b.y = a.y + to[i][1];
                b.speed = a.speed;
                if(b.x < 1 || b.y < 1 || b.x > n || b.y > n) continue;
                if(mp[b.x][b.y] == 'O') continue;
                if(vis[b.speed][b.x][b.y]) continue;
                vis[b.speed][b.x][b.y] = 1;
                b.step = a.step + b.speed;
                if(mp[b.x][b.y] == 'C') b.speed = 1;
                if(b.step <= k) q.push(b);
            }
        }
        printf("NO
    ");
    }
    int main(){
        int T;
        scanf("%d",&T);
        while(T--){
            scanf("%d%d",&n,&k);
            for(int i = 1;i <= n;i++){
                scanf("%s",mp[i] + 1);
                for(int j = 1;j <= n;j++){
                    if(mp[i][j] == 'S') x = i,y = j;
                }
            }
            memset(vis,0,sizeof(vis));
            BFS();
        }
        return 0;
    }
    
    


  • 相关阅读:
    Struts2 文件上传和下载
    Struts2的验证框架简单吗?
    Struts2防止重复提交
    Struts2的标签三大类是什么?
    Struts2的值栈和OGNL牛逼啊
    Struts2牛逼的拦截器,卧槽这才是最牛的核心!
    转载---C# 递归创建文件夹
    转载---C# 冻结 datagridview的列
    C# base64编码转成图片
    转载---C# 复制文件夹下的(内容)到指定文件夹中
  • 原文地址:https://www.cnblogs.com/KirinSB/p/9408793.html
Copyright © 2011-2022 走看看