zoukankan      html  css  js  c++  java
  • Codeforces 359E Neatness

    Neatnes

    dfs一下用set维护能不能走, 进入的时候点亮灯, 回溯的时候灭灯。

    #include<bits/stdc++.h>
    #define LL long long
    #define LD long double
    #define ull unsigned long long
    #define fi first
    #define se second
    #define mk make_pair
    #define PLL pair<LL, LL>
    #define PLI pair<LL, int>
    #define PII pair<int, int>
    #define SZ(x) ((int)x.size())
    #define ALL(x) (x).begin(), (x).end()
    
    using namespace std;
    
    const int N = 500 + 7;
    const int inf = 0x3f3f3f3f;
    const LL INF = 0x3f3f3f3f3f3f3f3f;
    const int mod = 1e9 + 7;
    const double eps = 1e-8;
    const double PI = acos(-1);
    
    template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
    template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;}
    template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
    template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;}
    
    int n, sx, sy;
    int on[N][N];
    bool vis[N][N];
    set<int> row[N], col[N];
    string path;
    
    bool check(int x, int y, int vx, int vy) {
        if(x + vx <= 0 || x + vx > n) return false;
        if(y + vy <= 0 || y + vy > n) return false;
        if(vis[x + vx][y + vy]) return false;
        if(!vy) {
            if(vx == 1) return upper_bound(ALL(col[y]), x) != col[y].end();
            else return lower_bound(ALL(col[y]), x) != col[y].begin();
        } else {
            if(vy == 1) return upper_bound(ALL(row[x]), y) != row[x].end();
            else return lower_bound(ALL(row[x]), y) != row[x].begin();
        }
    }
    
    void dfs(int x, int y) {
        vis[x][y] = true;
        if(!on[x][y]) {
            path.push_back('1');
            on[x][y] = true;
            row[x].insert(y);
            col[y].insert(x);
        }
        if(check(x, y, 0, 1)) {
            path.push_back('R');
            dfs(x, y + 1);
            path.push_back('L');
        }
        if(check(x, y, 0, -1)) {
            path.push_back('L');
            dfs(x, y - 1);
            path.push_back('R');
        }
        if(check(x, y, 1, 0)) {
            path.push_back('D');
            dfs(x + 1, y);
            path.push_back('U');
        }
        if(check(x, y, -1, 0)) {
            path.push_back('U');
            dfs(x - 1, y);
            path.push_back('D');
        }
        if(on[x][y]) {
            on[x][y] = false;
            path.push_back('2');
            row[x].erase(y);
            col[y].erase(x);
        }
    }
    
    int main() {
        cin >> n >> sx >> sy;
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                cin >> on[i][j];
                if(on[i][j]) {
                    row[i].insert(j);
                    col[j].insert(i);
                }
            }
        }
        dfs(sx, sy);
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
                if(on[i][j]) return puts("NO"), 0;
        cout << "YES" << "
    ";
        cout << path << "
    ";
        return 0;
    }
    
    /*
    */
  • 相关阅读:
    洛谷P1033 自由落体 题解
    尴尬
    UVA11988 【Broken Keyboard (a.k.a. Beiju Text)】:题解
    UVA101 The Blocks Problem 题解
    TCP的粘包和拆包问题及解决办法(C#)
    MIPS学习笔记(一)
    MySQL基础(一)
    博客园的标签怎么变了两下???
    nextInt()和nextLine()连用报错
    C++代码雨
  • 原文地址:https://www.cnblogs.com/CJLHY/p/10723733.html
Copyright © 2011-2022 走看看