codeforce1064 D. Labyrinth 双端队列 决策优化

codeforce1064 D. Labyrinth

给定矩阵,*表示障碍,向左最多L步向右最多R步,求其所有可能能到达的点的个数

双端队列 决策优先性

#include<bits/stdc++.h>
using namespace std;
const int MAX=2e3+5;
int n,m,a,b,c,d,ans;
char s[MAX][MAX];
struct P
{
    int x,y,l,r;
    P(int a1,int a2,int a3,int a4){x=a1;y=a2;l=a3;r=a4;}
};
deque<P>q;
void bfs()
{
    while(!q.empty()) q.pop_front();
    q.push_back(P(a,b,c,d));
    while(!q.empty())
    {
        P u=q.front();q.pop_front();
        int x_=u.x,y_=u.y;
        if(s[x_][y_]=='*') continue;
        s[x_][y_]='*';++ans;
        if(s[x_-1][y_]!='*'&&x_-1>0)
            q.push_front(P(x_-1,y_,u.l,u.r));
        if(s[x_+1][y_]!='*'&&x_+1<=n)
            q.push_front(P(x_+1,y_,u.l,u.r));
        if(u.r>0&&s[x_][y_+1]!='*'&&y_+1<=m)
            q.push_back(P(x_,y_+1,u.l,u.r-1));
        if(u.l>0&&s[x_][y_-1]!='*'&&y_-1>0)
            q.push_back(P(x_,y_-1,u.l-1,u.r));
    }
}
int main()
{
    while(~scanf("%d%d%d%d%d%d",&n,&m,&a,&b,&c,&d))
    {
        ans=0;
        for(int i=1;i<=n;++i) scanf("%s",s[i]+1);
        bfs();
        printf("%d\n",ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值