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
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分钟,只能在两端的楼梯上楼

且只有一层的灯全关闭才能上楼,问最小时间。最下面是一楼,最上面是顶楼,人初始

在(n,1)的位置

思路:dp dp[i][0]表示在i层左边上楼的花费,dp[i][1]表示在i层右边上楼的花费

注意一层全空的情况即可

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cmath> using namespace std; #define LL long long
const int inf=0x7fffffff; int dp[20][2];
char mp[20][200];
int l[20],r[20]; int main()
{
int n,m;
scanf("%d%d",&n,&m);
memset(l,0,sizeof l);
memset(r,0,sizeof r);
memset(dp,inf,sizeof dp);
for(int i=n-1; i>=0; i--)
{
scanf("%s",&mp[i]);
for(int j=0; j<m+2; j++)
{
if(mp[i][j]=='1')
r[i]=j;
if(mp[i][j]=='1'&&l[i]==0)
l[i]=j;
}
}
for(int i=n-1; i>=0; i--)
if(l[i]==0&&r[i]==0)
n--;
else
break;
if(n==1)
printf("%d",r[0]);
else
{
dp[0][0]=2*r[0]+1;
dp[0][1]=m+1+1;
for(int i=1; i<n-1; i++)
{
if(l[i]==0&&r[i]==0)
dp[i][0]=dp[i-1][0]+1,dp[i][1]=dp[i-1][1]+1;
else
{
dp[i][0]=min(dp[i-1][0]+2*r[i],dp[i-1][1]+m+1)+1;
dp[i][1]=min(dp[i-1][0]+m+1,dp[i-1][1]+(m+1-l[i])*2)+1;
}
} printf("%d\n",min(dp[n-2][0]+r[n-1],dp[n-2][1]+(m+1-l[n-1])));
} return 0;
}

Codeforces812B Sagheer, the Hausmeister 2017-06-02 20:47 85人阅读 评论(0) 收藏的更多相关文章

  1. Codeforces812A Sagheer and Crossroads 2017-06-02 20:41 139人阅读 评论(0) 收藏

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

  2. Codeforces812C Sagheer and Nubian Market 2017-06-02 20:39 153人阅读 评论(0) 收藏

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

  3. hadoop调优之一:概述 分类: A1_HADOOP B3_LINUX 2015-03-13 20:51 395人阅读 评论(0) 收藏

    hadoop集群性能低下的常见原因 (一)硬件环境 1.CPU/内存不足,或未充分利用 2.网络原因 3.磁盘原因 (二)map任务原因 1.输入文件中小文件过多,导致多次启动和停止JVM进程.可以设 ...

  4. Self Numbers 分类: POJ 2015-06-12 20:07 14人阅读 评论(0) 收藏

    Self Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22101   Accepted: 12429 De ...

  5. Debian自启动知识 2015-03-31 20:23 79人阅读 评论(0) 收藏

    Debian6添加了insserv用来代替update-rc.d.update-rc.d 就不多做介绍. Debian6里边要添加一个自动启动的服务需要先将启动脚本放在/etc/init.d,然后使用 ...

  6. UI基础:UIView(window,frame,UIColor,CGPoint,alpha,CGRect等) 分类: iOS学习-UI 2015-06-30 20:01 119人阅读 评论(0) 收藏

    UIView 视图类,视图都是UIView或者UIView子类 UIWindow 窗口类,用于展示视图,视图一定要添加window才能显示 注意:一般来说,一个应用只有一个window 创建一个UIW ...

  7. OC基础:OC 基本数据类型与对象之间的转换方法 分类: ios学习 OC 2015-06-18 20:01 11人阅读 评论(0) 收藏

    1.Foundation框架中提供了很多的集合类如:NSArray,NSMutableArray,NSSet,NSMutableSet,NSDictionary,NSMutableDictionary ...

  8. PAT甲 1001. A+B Format (20) 2016-09-09 22:47 25人阅读 评论(0) 收藏

    1001. A+B Format (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Calculate ...

  9. ZOJ2748 Free Kick 2017-04-18 20:40 40人阅读 评论(0) 收藏

    Free Kick Time Limit: 2 Seconds      Memory Limit: 65536 KB In a soccer game, a direct free kick is ...

随机推荐

  1. swoole的EventLoop学习

    我们先使用php来写一个socket的服务端.先从最开始的模型开始将起逐步引申到为何要使用eventloop 1.最简单的socket服务端,直接按照官方文档来执行 <?php $sock = ...

  2. 【解决办法--实测可行】Partition 1 does not start on physical sector boundary.

    新的硬盘使用fdisk进行划分的时候有提示Partition 1 does not start on physical sector boundary.后面按网上找的办法,在fdisk进行分区的时候, ...

  3. UI设计教程分享:电商网页页面设计常见表现手法

    1.手绘插画  场景.人物以及加上故事的创意绘画 会给人梦幻若隐若现的感觉,留下深刻的印象,适合做活动页面以及宣传自已的品牌 2.简约 颜色少于三色,背景以明度偏低的颜色为主,在信息大爆炸的时代,我们 ...

  4. npm run build出问题十分通用的解决方法

    1.C:\NanoFabric\52ABP\SPAHost\ClientApp\node_modules 原来的目录重命名为C:\NanoFabric\52ABP\SPAHost\ClientApp\ ...

  5. Exception 异常 输出的各个方法的区别

    try{ System.out.println(1/0); }catch(Exception e){ //System.out.println(e+""); //对象+字符串 = ...

  6. 63.delegate回调 和block回调

    效果:viewController里面放置一个按钮和Lab,点击按钮进入oneViewController(delegate回调)或者BlockViewController(block回调),两者控制 ...

  7. centos7 sqoop 1 搭建笔记

    1.require : java环境,hadoop,hive ,mysql2.下载解压sqoop13.设置环境变量 export SQOOP_HOME=/data/spark/bin/sqoop ex ...

  8. 【转】Centos yum 换源

    [1] 首先备份/etc/yum.repos.d/CentOS-Base.repo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/Cent ...

  9. 使用Hadoop API 压缩HDFS文件

    下篇解压缩:使用Hadoop API 解压缩 HDFS文件 起因: 集群磁盘剩余空间不足. 删除了存储在HDFS上的,一定时间之前的中间结果,发现并不能释放太多空间,查看计算业务,发现,每天的日志存在 ...

  10. 存储引擎中MYIASM是什么意思