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. Python 字符串(count)

    字符串 count:(python中的count()函数,从字面上可以知道,他具有统计功能) Python count() 方法用于统计字符串里某个字符出现的次数.可选参数为在字符串搜索的开始与结束位 ...

  2. Centos7 上安装 FastDFS

    1.安装gcc(编译时需要) FastDFS是C语言开发,安装FastDFS需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc yum install -y gcc ...

  3. c++文件中引用C代码

    下面提供一个比较完整的示例程序,一共有四个文件:main.cpp.test.c.test.h.test.hpp main.cpp #include "test.hpp" int m ...

  4. refused to Connection

    两种情况: .数据库账号密码错误 .mysql挂了

  5. 事务 TRANSACTION

    事务是数据库中一个但单独的执行单元(Unit),他通常由高级数据库操作语言(如SQL)或编程语言(如C++.Java)编写的用户程序的执行所引起.当在数据库中更改数据成功时,在事务中更改的数据便会提交 ...

  6. Java中多线程访问冲突的解决方式

    当时用多线程访问同一个资源时,非常容易出现线程安全的问题,例如当多个线程同时对一个数据进行修改时,会导致某些线程对数据的修改丢失.因此需要采用同步机制来解决这种问题. 第一种 同步方法 第二种 同步代 ...

  7. MySQL 快速复数据库的方法

    为了方便快速复制一个数据库,可以用以下命令将db1数据库的数据以及表结构复制到newdb数据库 创建新的数据库 #mysql -u root -p123456 mysql>CREATE DATA ...

  8. zz Alex's BLOG 串口连接

    using System; using System.Collections.Generic;using System.ComponentModel;using System.Data;using S ...

  9. java通过接口扩展枚举

    package com.hra.riskprice; import com.hra.riskprice.SysEnum.Factor_Type; import com.hra.riskprice.po ...

  10. Python终端彩色字体的输出

    实现过程: 终端的字符颜色是用转义序列控制的,是文本模式下的系统显示功能,和具体的语言无关. 转义序列是以ESC开头,即用\033来完成(ESC的ASCII码用十进制表示是27,用八进制表示就是033 ...