D. Labyrinth

http://codeforces.com/contest/1064/problem/D

bfs
用双端队列做,向上走向下走的节点加入队首,左右走的加到队尾,其余的都是正常bfs

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxN=2005;
int N,M,X,Y,ans;
char ma[maxN][maxN];
int dir[4][2]={1,0,-1,0,0,1,0,-1};
struct DM
{
    int x,y,l,r;
};
void bfs(int sx,int sy)
{
    deque<DM>que;
    que.push_back((DM){sx,sy,X,Y});
    ma[sx][sy]='*';
    while(!que.empty())
    {
        DM v=que.front();
        que.pop_front();
        ans++;
        for(int i=0;i<4;i++)
        {
            int xx=v.x+dir[i][0];
            int yy=v.y+dir[i][1];
            if(xx<1||xx>N||yy<1||yy>M||ma[xx][yy]=='*')continue;
            if(i<=1)que.push_front((DM){xx,yy,v.l,v.r}),ma[xx][yy]='*';
            if(i==2&&v.r>=1)que.push_back((DM){xx,yy,v.l,v.r-1}),ma[xx][yy]='*';
            if(i==3&&v.l>=1)que.push_back((DM){xx,yy,v.l-1,v.r}),ma[xx][yy]='*';
        }
    }

}
int main()
{
    int sx,sy;
    scanf("%d%d%d%d%d%d",&N,&M,&sx,&sy,&X,&Y);
    for(int i=1;i<=N;i++)scanf("%s",ma[i]+1);
    bfs(sx,sy);
    printf("%d\n",ans);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值