zoukankan      html  css  js  c++  java
  • codeforces 1064D 双端队列BFS

    双端队列BFS解决的就是路径权值可能为0的图最短路问题,权值为0插入队头,否则插入队尾。

    对于这个题,可以看作上下移动的路径的权值为0,左右移动权值为1,而且不能超过规定的步数。

    直接广搜求覆盖的点的数目即可。

    (场上我一般BFS被hack了)

    代码:

    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<iostream>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<map>
    #include<vector>
    #include<set>
    #include<bitset>
    #define INF 0x3f3f3f3f
    #define LL long long
    using namespace std;
    const int maxn=200010;
    int a[maxn];
    char s[2010][2010];
    bool v[2010][2010];
    int n,m,r,c,rr,cc;
    int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
    bool valid(int x,int y){
        return x>=1&&x<=n&&y>=1&&y<=m&&s[x][y]!='*';
    }
    struct node{
        int x,y,cnt_l,cnt_r;
    };
    deque<node> q;
    void bfs(void){
        node tmp=(node){r,c,rr,cc};
        node tmp1;
        int ans=0;
        q.push_back(tmp);
        while(!q.empty()){
            tmp=q.front();
            q.pop_front();
            int x=tmp.x,y=tmp.y;
            if(v[x][y])continue;
            v[x][y]=1;
            ans++;
            for(int i=0;i<4;i++){
                tmp1=tmp;
                int tx=x+dx[i];
                int ty=y+dy[i];
                if(valid(tx,ty)){
                    if(i==1){
                        if(tmp.cnt_r>0){
                            tmp1.cnt_r--;
                            tmp1.x=tx;
                            tmp1.y=ty;
                            q.push_back(tmp1);
                        }
                    }
                    else if(i==3){
                        if(tmp.cnt_l>0){
                            tmp1.cnt_l--;
                            tmp1.x=tx;
                            tmp1.y=ty;
                            q.push_back(tmp1);
                        }
                    }
                    else{
                        tmp1.x=tx;
                        tmp1.y=ty;
                        q.push_front(tmp1);
                    }
                }
            } 
        }
        printf("%d
    ",ans);
    }
    int main(){
        scanf("%d%d",&n,&m);
        scanf("%d%d",&r,&c);
        scanf("%d%d",&rr,&cc);
        for(int i=1;i<=n;i++)
            scanf("%s",s[i]+1);
        bfs(); 
    }
  • 相关阅读:
    fedora20安装hadoop-2.5.1
    超简单fedora20(linux)下JDK1.8的安装
    解决A program file was not specified in the launch configuration.问题
    java中的四则运算
    spring util命名空间
    java中常用的数据加密算法
    C语言实现栈
    百度ueditor富文本编辑器的使用
    C#进程管理程序实现
    ABP领域层-仓储
  • 原文地址:https://www.cnblogs.com/pkgunboat/p/9788895.html
Copyright © 2011-2022 走看看