zoukankan      html  css  js  c++  java
  • 2021暑期牛客多校8-F Robots

    2021暑期牛客多校8-F Robots

    思路

    对于第一和第二种机器,因为方向唯一,暴力即可

    对于第三种机器,如果直接暴力的话时间和空间都无法承受,考虑优化一下

    如果从点 A 可以到达点 B ,那么就可以从点 A 到达点 B 右或下方不是障碍的地方

    换句话说,如果从点 B 左或上方的某一点 C 可以到达点 B ,那么所有能到点 C 的点也一定能到达点 B

    所以说能到点 B 的点的点集由点 B 本身和它的左或上决定

    此时用类似轮廓线dp的方法去维护可行点集,复杂度就降为了 (Theta(m*nm))

    可以用 bitset 来维护

    代码

    #include <bits/stdc++.h>
    using namespace std;
    
    typedef long long ll;
    typedef unsigned long long ull;
    typedef const int& cint;
    
    const int mod = 1e9+7;
    const int inf_int = 0x7fffffff;
    const ll inf_ll = 0x7fffffffffffffff;
    const double ept = 1e-9;
    
    int n, m, q;
    char mp[505][505];
    bitset<500*500+1> r[501];
    bool ans[500500];
    
    struct node { int x, y, id; };
    vector<node> e[505][505];
    int dfs(cint x, cint y, cint dx, cint dy) {
        if(x > n) return x-1;
        if(y > m) return y-1;
        if(mp[x][y] == '1') return (dx == 1 ? x : y)-1;
        return dfs(x+dx, y+dy, dx, dy);
    }
    
    int main() {
        cin >> n >> m;
        for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) cin >> mp[i][j];
        cin >> q;
        int a, x1, x2, y1, y2;
        for(int i=1; i<=q; i++) {
            cin >> a >> x1 >> y1 >> x2 >> y2;
            if(a == 1) { if(y1 == y2 && x1 <= x2) ans[i] = (x2 <= dfs(x1, y1, 1, 0)); }
            else if(a == 2) { if(x1 == x2 && y1 <= y2) ans[i] = (y2 <= dfs(x1, y1, 0, 1)); }
            else e[x2][y2].push_back( {x1, y1, i} );
        }
        for(int i=1; i<=n; i++) {
            for(int j=1; j<=m; j++) {
                if(mp[i][j] == '0') {
                    r[j][(i-1)*m+j] = 1;
                    r[j] |= r[j-1];
                    if(e[i][j].size()) for(node x:e[i][j]) ans[x.id] = r[j][m*(x.x-1)+x.y];
                }
                else r[j].reset();
            }
        }
        for(int i=1; i<=q; i++)
            if(ans[i]) cout << "yes" << endl;
            else cout << "no" << endl;
        return 0;
    }
    
  • 相关阅读:
    java练习题2
    java练习题
    java输入输出
    字符集
    eclipse快捷键
    类和对象练习-people
    类和对象-三角形
    权限修饰符-输出求和阶乘
    权限修饰符-练习
    权限修饰符-father&&son
  • 原文地址:https://www.cnblogs.com/ullio/p/15139540.html
Copyright © 2011-2022 走看看