Codeforces 1064D Labyrinth(双端队列BFS)

题意:

给一个图,"*"不可以走,给你一个起点,限制向左走L次,向右走R次,上下不限制,问你最多可以走到多少个格子

思路:

BFS,每次将上下走的策略加入队首,左右加入队尾,(相当于上下走比左右走优先级大的优先队列),这样可以保证先到某一格时剩余的疲劳度是最大的

 

但是,,如果上下左右都限制,该咋办啊。。有没有大佬能给个思路啊

 

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional>
    
#define fst first
#define sc second
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1
#define lowbit(x) ((x)&(-x)) 

using namespace std;

typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL;

const db eps = 1e-6;
const int mod = 998244353;
const int maxn = 2e6+100;
const int maxm = 2e6+100;
const int inf = 0x3f3f3f3f;
const db pi = acos(-1.0);

int n, m;
int rx, ry;
int L, R;
char a[2000 + 10][2000 + 10];
int vis[2000 + 10][2000 + 10];
struct node{
    int l, r;
    int x, y;
    node(int x=0, int y=0, int l=0, int r=0):x(x),y(y),l(l),r(r){}
};
deque<node>q;
int main() {
    mem(vis, 0);
    scanf("%d %d", &n, &m);
    scanf("%d %d", &rx, &ry);
    scanf("%d %d", &L, &R);
    for(int i = 1; i <= n; i++)scanf("%s",a[i]+1);
    q.push_front(node(rx,ry,L,R));
    while(!q.empty()){
        node p = q.front();
        q.pop_front();
        int x = p.x;
        int y = p.y;
        int l = p.l;
        int r = p.r;
        //printf("%d %d %d %d\n", x,y,l,r);
        if(vis[x][y])continue;
        vis[x][y]=1;
        if(l&&y-1>=1&&a[x][y-1]=='.'){
            q.push_back(node(x,y-1,l-1,r));
        }
        if(r&&y+1<=m&&a[x][y+1]=='.'){
            q.push_back(node(x,y+1,l,r-1));
        }

        if(x+1<=n&&a[x+1][y]=='.'){
            q.push_front(node(x+1,y,l,r));
        }
        if(x-1>=1&&a[x-1][y]=='.'){
            q.push_front(node(x-1,y,l,r));
        }
        
    }
    int ans = 0;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            if(vis[i][j]==1){ans++;
            }
        }
    }
    printf("%d", ans);
    return 0;

}
/*
6 5
1 5
5 1
.....
.***.
...*.
*.**.
*.**.
*....
 */

 

posted @ 2018-10-15 10:03  wrjlinkkkkkk  阅读(269)  评论(0编辑  收藏  举报