• 【codeforces 812B】Sagheer, the Hausmeister


    【题目链接】:http://codeforces.com/contest/812/problem/B

    【题意】

    一个老大爷在一楼;
    然后他有n楼的灯要关(最多n楼);
    每楼有m个房间;
    给出每个房间的灯的信息(亮或不亮)
    然后他移动到相邻的房间,或者是移动到上一层都花费一分钟;
    每一层的灯没有全部关掉之前,他不会往上层走;
    (每层的最左和最右是两个楼道,那两个地方才能往上走);
    问你最小时间;

    【题解】

    dfs;
    每层楼有两种情况;
    从左楼道上楼,从右楼道上楼;
    然后记录最小的,有灯是开着的楼层号;
    最后到了那一层以后,就不用再往上了;
    在记录的时候,只要记录每一层的最左边和最右边的有亮的灯的房间号就好;
    (因为肯定是一直往一个方向走的,不然肯定更费时);
    根据这一次要从左楼道上还是右楼道上,增加相应的时间;

    【Number Of WA

    0

    【完整代码】

    #include <bits/stdc++.h>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    #define ms(x,y) memset(x,y,sizeof x)
    #define Open() freopen("F:\rush.txt","r",stdin)
    #define Close() ios::sync_with_stdio(0),cin.tie(0)
    
    typedef pair<int,int> pii;
    typedef pair<LL,LL> pll;
    
    const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
    const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
    const double pi = acos(-1.0);
    const int N = 20;
    const int M = 1e2+10;
    const int INF = 0x3f3f3f3f;
    
    int l[N],r[N],n,m,ans = INF,stop;
    char s[M];
    
    void dfs(int dep,int pos,int time){
        if (dep==stop){
            if (pos==0){
                ans = min(ans,time+r[dep]);
            }
            else{
                ans = min(ans,time+m-l[dep]+1);
            }
            return;
        }
        if (pos==0){
            if (r[dep]==-1){
                dfs(dep-1,pos,time+1);
            }
            else{
                dfs(dep-1,pos,time+r[dep]*2+1);
                dfs(dep-1,m+1,time+m+1+1);
            }
        }
        else{
            if (r[dep]==-1){
                dfs(dep-1,pos,time+1);
            }
            else{
                dfs(dep-1,pos,time+(m-l[dep]+1)*2+1);
                dfs(dep-1,0,time+m+1+1);
            }
        }
    }
    
    int main(){
        //Open();
        Close();//scanf,puts,printf not use
        //init??????
        cin >> n >> m;
        rep1(i,1,n){
            cin >>s;
            l[i] = r[i] = -1;
            rep1(j,1,m)
                if (s[j]=='1'){
                    l[i] = j;
                    break;
                }
            rep2(j,m,1)
                if (s[j]=='1'){
                    r[i] = j;
                    break;
                }
        }
        stop=1;
        while (stop<=n && l[stop]==-1) stop++;
        if (stop>n){
            cout <<0<<endl;
            return 0;
        }
        dfs(n,0,0);
        cout << ans << endl;
        return 0;
    }
  • 相关阅读:
    【BZOJ4318】OSU! 期望DP
    【BZOJ2956】模积和 分块
    【BZOJ4443】[Scoi2015]小凸玩矩阵 二分+二分图最大匹配
    【BZOJ2253】[2010 Beijing wc]纸箱堆叠 cdq分治
    【BZOJ3555】[Ctsc2014]企鹅QQ hash
    【BZOJ3238】[Ahoi2013]差异 后缀数组+单调栈
    【BZOJ2287】【POJ Challenge】消失之物 背包动规
    【BZOJ4517】[Sdoi2016]排列计数 组合数+错排
    【BZOJ4551】[Tjoi2016&Heoi2016]树 并查集
    【BZOJ2783】[JLOI2012]树 DFS+栈+队列
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626288.html
Copyright © 2020-2023  润新知