B. Sagheer, the Hausmeister

B. Sagheer, the Hausmeister
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off.

The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms.

Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights.

Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.

Input

The first line contains two integers n and m (1 ≤ n ≤ 15 and 1 ≤ m ≤ 100) — the number of floors and the number of rooms in each floor, respectively.

The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor.

The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.

Output

Print a single integer — the minimum total time needed to turn off all the lights.

Examples
input
2 2
0010
0100
output
5
input
3 4
001000
000010
000010
output
12
input
4 3
01110
01110
01110
01110
output
18

嗯,直接dfs,每次枚举从哪个楼梯上就可以了。

不过,我踩了一个坑点就是如果上面的层没有灯了,就不需要向上搜索了。

#include <cstdio>
#include <queue>
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;
const int MAXN = 1e5+7;
const int inf = 1e9;
int n,m;
char tu[20][105];
int l[20],r[20];
int hang[20];
int ans = inf,limit;
void dfs(int x,int dir,int sum)//分别表示层数,当前层是从哪个阶梯开始的,以及到达这层已经花费了多少时间
{
    if(sum > ans)return;
    if(x == limit)
    {
        if(dir)ans = min(ans,sum + m-1-l[x]);
        else ans = min(ans,sum + r[x]);
        return ;
    }
    if(dir == 0)
    {
        dfs(x-1,0,sum+r[x]*2+1);
        dfs(x-1,1,sum+m);
    }
    else
    {
        dfs(x-1,0,sum+m);
        dfs(x-1,1,sum+(m-1-l[x])*2+1);
    }
}


int main()
{
    scanf("%d%d",&n,&m);
    m += 2;
    for(int i = 0 ; i < n ; ++i)
    {
        l[i] = m-1;//默认这两个,因为没有灯的时候也需要符合搜索条件譬如sum + r[x],sum+(m-1-l[x])
        r[i] = 0;
        scanf("%s",tu[i]);
        for(int j = 0 ; j < m ; ++j)
        {
            if(tu[i][j] == '1')
            {
                hang[i]++;
                l[i] = min(l[i],j);
                r[i] = max(r[i],j);
            }
        }
    }
    //利用前缀和计算出只需要搜索到哪一层即可
    for(int i = 1 ; i < n ;++i)hang[i] += hang[i-1];
    limit = 0;
    while(limit < n-1 && !hang[limit])limit++;
    dfs(n-1,0,0);
    printf("%d\n",ans);
    return 0;
}






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值