Codeforces Round #516 (Div. 2) D. Labyrinth ---- BFS+思维

题目传送门

做法:

  • 优先选取列,然后在向左右扩展。
  • 然鹅,有思路,却不会处理,看了本场Rank1,有双端队列巧妙处理列和行的优先级,才发现处理更简洁+易懂。ORZ
  • 我们优先将列放到队首,因为列是不需要消耗步数的,然后再将左右放到队尾,然后依次访问+标记,即可。

AC代码:

#include<bits/stdc++.h>
#define IO          ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define pb(x)       push_back(x)
#define sz(x)       (int)(x).size()
#define sc(x)       scanf("%d",&x)
#define pr(x)       printf("%d\n",x)
#define abs(x)      ((x)<0 ? -(x) : x)
#define all(x)      x.begin(),x.end()
#define mk(x,y)     make_pair(x,y)
#define debug       printf("!!!!!!\n")
#define fin         freopen("in.txt","r",stdin)
#define fout        freopen("out.txt","w",stdout)
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int mod = 1e9+7;
const double PI = 4*atan(1.0);
const int maxm = 1e8+5;
const int maxn = 2e3+5;
const int INF = 0x3f3f3f3f;
const ll LINF = 1ll<<62;
int n,m,sx,sy,nx,ny;
char mmap[maxn][maxn];
bool vis[maxn][maxn];
struct node
{
    int x,y;
    int nx,ny;
    node(int x,int y,int nx,int ny){
        this->x = x;
        this->y = y;
        this->nx = nx;
        this->ny = ny;
    }
};
deque<node> q;
int bfs()
{
    int cnt = 0;
    while(!q.empty())
    {
        node tmp = q.front();
        q.pop_front();
        int x = tmp.x,y = tmp.y;
        int nx = tmp.nx,ny = tmp.ny;
        if(vis[x][y]) continue;
        vis[x][y] = 1;
        cnt++;
        if(x-1>0 && mmap[x-1][y]!='*') q.push_front(node(x-1,y,nx,ny));
        if(x+1<=n && mmap[x+1][y]!='*') q.push_front(node(x+1,y,nx,ny));
        if(y-1>0 && mmap[x][y-1]!='*' && nx) q.push_back(node(x,y-1,nx-1,ny));
        if(y+1<=m && mmap[x][y+1]!='*'&& ny) q.push_back(node(x,y+1,nx,ny-1));
    }
    return cnt;
}
int main()
{
    // fin;
    IO;
    cin>>n>>m>>sx>>sy>>nx>>ny;
    for(int i=1;i<=n;i++)
        cin>>mmap[i]+1;
    q.push_front(node(sx,sy,nx,ny));
    cout<<bfs()<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值