Codeforces 812 B Sagheer, the Hausmeister

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

题意:题目中的意思就是说有一栋楼有n层每层有m个房间,两边各有一个楼梯,上下楼梯需要的时间是1分钟,从一个房间走到相邻的房间的时间是1分钟,这栋楼里有一些房间的灯是亮着的,你需要把这些灯给关掉,问你最少需要的时间是多少,你每次都是从最下面那层的左边出发,最后不需要回到起点。

题解:其实直接dfs所有的状态就好了(详细还是看代码)

#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#define inf 0x3f3f3f3f
#define LL long long
using namespace std;
int n, m;
char mapp[20][110];
int dfs(int cnt, int pos, int temp) {//cnt表示楼层,pos表示是哪边的楼梯,temp表示的是上楼要走多少步
    if (cnt < 0) {
        return 0;
    }
    int ans = 0;
    if (pos) {
        for (int i = m+2; i >= 0; i--) {
            if (mapp[cnt][i] == '1') {
                ans += pos - i + temp;
                pos = i;
                temp = 0;
            }
        }
    }
    else {
        for (int i = 0; i < m+2; i++) {
            if (mapp[cnt][i] == '1') {
                ans += i - pos + temp;
                pos = i;
                temp = 0;
            }
        }
    }
    ans += min(dfs(cnt - 1, 0, temp + pos + 1), dfs(cnt - 1, m + 1, temp + m + 2 - pos));
    return ans;
}
int main() {
    cin.sync_with_stdio(false);
    while (cin >> n >> m) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m + 2; j++) {
                cin >> mapp[i][j];
            }
        }
        cout << dfs(n-1,0,0) << endl;
    }
    return 0;
}

2017-06-02 12:59:06 

posted @ 2017-06-02 12:59  ガ落涙『不變』  阅读(217)  评论(0编辑  收藏  举报