Codeforces 812B-Sagheer, the Hausmeister (DFS暴力)

 

B. Sagheer, the Hausmeister

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

 

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

Copy

2 2
0010
0100

output

Copy

5

input

Copy

3 4
001000
000010
000010

output

Copy

12

input

Copy

4 3
01110
01110
01110
01110

output

Copy

18

Note

In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs.

In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor.

In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor.

 

 

解题思路:从最左下的楼梯开始,在没有找完所有1之前,进入每一层都能选择两种方案:关灯后从左边楼梯去上一层,或者从右边楼梯去。DFS遍历所有情况!

#include<bits/stdc++.h>
#define rep(i,j,k) for(int i=j;i<k;i++)
#define per(i,j,k) for(int i=j;i<=k;i++)
#define IO std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); 
using namespace std;
const int INF = 0x3f3f3f3f;
int maps[16][101];
int r[16];
int l[16]; 
int cnm[16];
int ans;
int n,m;
int num;
void init()
{
    num = 0;
    per(i,0,n)  
    {
        r[i] = 0;
        l[i] = m + 1; 
        cnm[i] = 0;
    }
}
void DFS(int f,int cost, int tot,int dr)
{	
    if(!dr)  
    {
        if(tot+cnm[f] == num)  
        {
            ans = min(ans,cost + r[f]);     
            return ;
        }
        DFS(f-1,cost + r[f]*2 +1,tot+cnm[f],0);  //若还没找完所有1  再上一层f-1
        DFS(f-1,cost + m + 2,tot+cnm[f], 1);  
    }
    else 
    {
        if(tot+cnm[f] == num)  
        {
            ans = min(ans,cost + (m + 1 - l[f]) );    
            return ;
        }
        DFS(f-1,cost + (m + 1 - l[f])*2 + 1,tot+cnm[f], 1); 
        DFS(f-1,cost + m + 2, tot+cnm[f], 0);    
    }
}
int main(void)
{
    IO 
    cin>>n>>m;
    init();
    per(i,1,n)
    {
        string s;
        cin>>s;
        per(j,1,m)  
        {
            int t = s[j] - '0';
            if(t)
            {
	        num++;
                cnm[i]++;
               if(l[i]==m+1) r[i] = l[i] = j;  //存储最左边的1
               else  r[i] = j;    //存储最右边的1
            }
           // maps[i][j+1] =  t ;   
        }
    }
    if(!num) 
    {
    	cout<<num<<endl;
    	return 0; 
    }    
    ans = INF;
    DFS(n,0,0,0); //在左边楼梯
    //DFS(n,m+2,cnm[n],1); //右边楼梯上
    cout<<ans<<endl; 
}       

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值