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 

转载于:https://www.cnblogs.com/xjh-shin/articles/6932753.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值