time limit per test  1 second
memory limit per test  256 megabytes
 

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 andm + 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.

 
input
2 2
0010
0100
output
5
input
3 4
001000
000010
000010
output
12
input
4 3
01110
01110
01110
01110
output
18
Note:
In the first example, Sagheer will go to room in the ground floor, then he will go to room 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.

Note


今天这题真是让人脑洞大开! (写之前先容我感慨一下)
题目大意:
     这题说的是,给定一个n*(m+2)的)01矩阵,表示一栋n层的大楼每层m个房间第一列和最后一列全部是0代表这栋楼两边的楼梯
     1代表这个房间的灯是亮的 现在有一个人想要关掉所有的灯 他刚开始在(n,1)走到相邻的房间需要1秒,上楼需要一秒,当一个楼层
     中所有的灯都是关闭的时候,他直接去下一个楼层。最后不需要回到起点,问需要的最少时间?
解题思路:
     不得不佩服网上的大佬们,当时让我束手无策的一题,大佬们竟然能用辣么多方法(变着花样的解出来) 、
     废话少说 下面我来记录一下我理解的一种思路
     主要思想的DP 定义一个dp数组dp[i][0]表示在第i层去i+1层的时候走的左边的楼梯,dp[i][1]表示走右边的楼梯
     每一层先找最左边亮灯的房间和最右边亮灯的房间,然后dp出走左右两个楼梯这两种情况的最优情况
     还有一点需要注意的是:要特殊判断一下每一层全是0的情况(全是0直接去下一层) AC代码:
 #include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int main ()
{
int n,m,i,j;
char s[][];
int left[],right[],dp[][];
while (~scanf("%d%d",&n,&m))
{
memset(left,,sizeof(left));
memset(right,,sizeof(right));
memset(dp,,sizeof(dp)); for (i = n-; i >= ; i --){ // 找出每一层中最左和最右边亮灯的房间地址
scanf("%s",s[i]);
for (j = ; j < m+; j ++){
if (s[i][j] == '')
right[i] = j;
if (s[i][j] == '' && left[i] == )
left[i] = j;
}
}
for(i = n-; i >= ; i --){
if (left[i]== && right[i]==)
n --;
else
break;
}
if (n == ){ // 没有亮灯的房间
printf("0\n");
continue;
}
else if(n == ){ // 只有第一层有亮灯的房间
printf("%d\n",right[]);
}
else{
dp[][] = *right[]+; // 初始化第一层所花费的时间
dp[][] = m+;
for (i = ; i < n-; i ++){
if (left[i]== && right[i]==){ // 该层所有房间的灯全是灭的
dp[i][] = dp[i-][]+;
dp[i][] = dp[i-][]+;
}
else{
dp[i][] = min(dp[i-][]+*right[i],dp[i-][]+m+)+;
dp[i][] = min(dp[i-][]+m+,dp[i-][]+*(m+-left[i]))+;
}
}
printf("%d\n",min(dp[n-][]+right[n-],dp[n-][]+m+-left[n-]));
} }
return ;
}

向大佬看齐! 刷题! 刷题!! 刷题!!!

Codeforces Round #417 B. Sagheer, the Hausmeister的更多相关文章

  1. Codeforces Round #417 C. Sagheer and Nubian Market

    C. Sagheer and Nubian Market time limit per test  2 seconds memory limit per test  256 megabytes   O ...

  2. Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister —— DP

    题目链接:http://codeforces.com/problemset/problem/812/B B. Sagheer, the Hausmeister time limit per test ...

  3. [Codeforces Round#417 Div.2]

    来自FallDream的博客,未经允许,请勿转载,谢谢. 有毒的一场div2 找了个1300的小号,结果B题题目看错没交  D题题目剧毒 E题差了10秒钟没交上去. 233 ------- A.Sag ...

  4. Codeforces Round #417 (Div. 2)A B C E 模拟 枚举 二分 阶梯博弈

    A. Sagheer and Crossroads time limit per test 1 second memory limit per test 256 megabytes input sta ...

  5. Codeforces Round #417 (Div. 2) 花式被虐

    A. Sagheer and Crossroads time limit per test 1 second memory limit per test 256 megabytes input sta ...

  6. codeforces round 417 div2 补题 CF 812 A-E

    A Sagheer and Crossroads 水题略过(然而被Hack了 以后要更加谨慎) #include<bits/stdc++.h> using namespace std; i ...

  7. Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister

    http://codeforces.com/contest/812/problem/B 题意: 有n层楼,每层楼有m个房间,1表示灯开着,0表示灯关了.最两侧的是楼梯. 现在每从一个房间移动到另一个房 ...

  8. 【动态规划】Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister

    预处理每一层最左侧的1的位置,以及最右侧的1的位置. f(i,0)表示第i层,从左侧上来的最小值.f(i,1)表示从右侧上来. 转移方程请看代码. #include<cstdio> #in ...

  9. Codeforces Round #417 (Div. 2) C. Sagheer and Nubian Market

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

随机推荐

  1. FJWC2019 最短路

    题目描述 有一张无向图,开始的时候所有边权为1,所有点没有权值,现在给定一个整数k,表示可以将k个点的点权设置为1,求点0到n-1的最短路最长是多少 Solution 网络流好题[然而本蒟蒻还是不会] ...

  2. Mac下像Windows那样带有预览图的快速切换-HyperSwitch

    这东西是免费的,他家还出了一个HyperDock的收费软件. 下载:https://bahoom.com/hyperswitch/get 离线版本:(链接: https://pan.baidu.com ...

  3. 基于angularJS的表单验证练习

    今天看了一下angularJS的表单验证,看的有点云里雾里(也有可能是雾霾吸多了),于是做了一个小练习来巩固一下. html: <div ng-controller="Aaa" ...

  4. 深入理解系列之 float

    float的设计初衷: 仅仅是为了实现文字环绕效果 float的感性认知: 包裹性: 收缩:元素应用了float后,宽度收缩,紧紧地包裹住内容(即元素的宽度收缩到元素内的内容的宽度大小 坚挺:原来没有 ...

  5. CentOS6.4将MySQL5.1升级至5.5.36

    1.为了安全期间,首先需要备份原有数据 2.卸载原有MySQL,先停止原有的MySQL服务,再查找 find / -name mysql [root@qxyw /]# find / -name mys ...

  6. InnoDB的行记录格式, Compact, Redundant, Compressed, Dynamic

    InnoDB存储引擎和大多数数据库一样(如Oracle和Microsoft SQL Server数据库),记录是以行的形式存储的.这意味着页中保存着表中一行行的数据.到MySQL 5.1时,InnoD ...

  7. python解决处理中文的问题

    脚本开头添加默认编码 python源码中出现了中文字符或要处理中文字符,运行时会出现错误,解决方法是,开头加入字符编码声明: #! /usr/bin/env python # -*- coding:u ...

  8. 虚拟机下的zookeeper集群安装

    ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件.它是一个为分布式应用提供一致性服务的软件,提供的功 ...

  9. HTML5--(1)兼容前缀+结构性标签+语义化标签

    一.兼容前缀+兼容写法 兼容前缀 1.HTML5有部分类容兼容到IE9,IE8及以下完全不兼容的内容不再考虑. 2.部分内容需要加兼容前缀 a)     -webkit- 兼容谷歌 b)     -m ...

  10. java 基础 --- java8 HashMap

    问题 : HashMap 容量大小 (capacity)为什么为 2n HashMap 是线程安全的吗,为什么 HashMap 既然有hash进行排位还需要equals()作用是什么   文章部分图片 ...