zoukankan      html  css  js  c++  java
  • CodeForces 1063B. Labyrinth 性质

    给定$n *m$的格子

    询问从$(r, c)$开始最多向左走$x$步,向右走$y$步

    询问有多少个格子可以从$(r, c)$到达

    有障碍物,$n, m leqslant 2 * 10^3$

    对于一个点$(x, y)$,可以发现$(r, c)$到$(x, y)$的一条向左走的步数和向右走的步数之和最小的路径可以使得向左走和向右走最优

    感性理解是如果比这个大的话,那么必定向左走和向右走的步数同时都要增加

    那么带上向左走的步数和向右走的步数来跑$bfs$即可

    注意上下之间的权值为$0$

    可以选择将上下缩成一个点或者跑$01$bfs

    代码用了一个队列 + 栈来实现双端队列,感觉$stl$的太丑了...

    复杂度$O(nm)$

    #include <vector>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    namespace remoon {
        #define re register
        #define de double
        #define le long double
        #define ri register int
        #define ll long long
        #define pii pair<int, int>
        #define mp make_pair
        #define pb push_back
        #define tpr template <typename ra>
        #define rep(iu, st, ed) for(ri iu = st; iu <= ed; iu ++)
        #define drep(iu, ed, st) for(ri iu = ed; iu >= st; iu --)    
        extern inline char gc() {
            static char RR[23456], *S = RR + 23333, *T = RR + 23333;
            if(S == T) fread(RR, 1, 23333, stdin), S = RR;
            return *S ++;
        }
        inline int read() {
            int p = 0, w = 1; char c = gc();
            while(c > '9' || c < '0') { if(c == '-') w = -1; c = gc(); }
            while(c >= '0' && c <= '9') p = p * 10 + c - '0', c = gc();
            return p * w;
        }
        int wr[50], rw;
        #define pc(iw) putchar(iw)
        tpr inline void write(ra o, char c = '
    ') {
            if(!o) pc('0');
            if(o < 0) o = -o, pc('-');
            while(o) wr[++ rw] = o % 10, o /= 10;
            while(rw) pc(wr[rw --] + '0');
            pc(c);
        }
        tpr inline void cmin(ra &a, ra b) { if(a > b) a = b; }
        tpr inline void cmax(ra &a, ra b) { if(a < b) a = b; } 
        tpr inline bool ckmin(ra &a, ra b) { return (a > b) ? a = b, 1 : 0; }
        tpr inline bool ckmax(ra &a, ra b) { return (a < b) ? a = b, 1 : 0; }
    }
    using namespace std;
    using namespace remoon;
    
    #define sid 2005
    #define aid 4005000
    
    int n, m, r, c, x, y;
    int vis[sid][sid];
    char s[sid][sid];
    
    inline char gch() {
        char c = gc();
        while(c != '*' && c != '.') c = gc();
        return c;
    }
    
    struct node {
        int x, y, l, r;
        node() {}
        node(int x, int y, int l, int r) : x(x), y(y), l(l), r(r) {}
    } q[aid], st[aid];
    
    int nx[4] = { 0, 0, 1, -1 };
    int ny[4] = { 1, -1, 0, 0 };
    
    inline void bfs() {
        vis[r][c] = 1;
        int fr = 1, to = 0, top = 0;
        q[++ to] = node(r, c, 0, 0);
        while(fr <= to || top) {
            node p;
            if(top) p = st[top --];
            else p = q[fr ++];
            int px = p.x, py = p.y, pl = p.l, pr = p.r;
            rep(i, 0, 3) {
                int dx = px + nx[i], dy = py + ny[i], dl = pl, dr = pr;
                if(dx < 1 || dx > n || dy < 1 || dy > m) continue;
                if(i == 0) dr = pr + 1;
                if(i == 1) dl = pl + 1;
                if(dl > x || dr > y || vis[dx][dy] || s[dx][dy] == '*') continue;
                vis[dx][dy] = 1;
                if(i == 0 || i == 1) q[++ to] = node(dx, dy, dl, dr);
                else st[++ top] = node(dx, dy, dl, dr);
            }
        }
        int ans = 0;
        rep(i, 1, n) rep(j, 1, m) ans += vis[i][j];
        write(ans);
    }
    
    int main() {
        n = read(); m = read();
        r = read(); c = read();
        x = read(); y = read();
        rep(i, 1, n) rep(j, 1, m) s[i][j] = gch();
        bfs();
        return 0;
    }
  • 相关阅读:
    Python学习笔记第二十三周(Flask架构)
    Python学习笔记第二十二周(前端知识点补充)
    Python学习笔记第二十一周
    Python学习笔记第二十周
    Python学习笔记第十九周
    Python学习笔记第十八周
    Python学习笔记第十七周
    Python学习笔记第十六周
    python完成九九乘法表
    python
  • 原文地址:https://www.cnblogs.com/reverymoon/p/9791155.html
Copyright © 2011-2022 走看看