Codeforces812B Sagheer, the Hausmeister 2017-06-02 20:47 85人阅读 评论(0) 收藏
1 second
256 megabytes
standard input
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.
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.
Print a single integer — the minimum total time needed to turn off all the lights.
2 2
0010
0100
5
3 4
001000
000010
000010
12
4 3
01110
01110
01110
01110
18
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) 收藏的更多相关文章
- 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 ...
- 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 ...
- hadoop调优之一:概述 分类: A1_HADOOP B3_LINUX 2015-03-13 20:51 395人阅读 评论(0) 收藏
hadoop集群性能低下的常见原因 (一)硬件环境 1.CPU/内存不足,或未充分利用 2.网络原因 3.磁盘原因 (二)map任务原因 1.输入文件中小文件过多,导致多次启动和停止JVM进程.可以设 ...
- Self Numbers 分类: POJ 2015-06-12 20:07 14人阅读 评论(0) 收藏
Self Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22101 Accepted: 12429 De ...
- Debian自启动知识 2015-03-31 20:23 79人阅读 评论(0) 收藏
Debian6添加了insserv用来代替update-rc.d.update-rc.d 就不多做介绍. Debian6里边要添加一个自动启动的服务需要先将启动脚本放在/etc/init.d,然后使用 ...
- UI基础:UIView(window,frame,UIColor,CGPoint,alpha,CGRect等) 分类: iOS学习-UI 2015-06-30 20:01 119人阅读 评论(0) 收藏
UIView 视图类,视图都是UIView或者UIView子类 UIWindow 窗口类,用于展示视图,视图一定要添加window才能显示 注意:一般来说,一个应用只有一个window 创建一个UIW ...
- OC基础:OC 基本数据类型与对象之间的转换方法 分类: ios学习 OC 2015-06-18 20:01 11人阅读 评论(0) 收藏
1.Foundation框架中提供了很多的集合类如:NSArray,NSMutableArray,NSSet,NSMutableSet,NSDictionary,NSMutableDictionary ...
- 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 ...
- 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 ...
随机推荐
- C# 一段通用的写log 日志的好程序
public void Write(string text) { FileStream fs = new FileStream(Application.StartupPath+"/log.t ...
- (转)wcf项目程序调试
由于使用分布式开发,因此在调试时,要分为客户端调试和服务端调试两种情况,下面就对这两种情况的调试步骤分别加以详细说明 调试客户端的页面代码 当仅仅需要调试客户端代码时,按照以下步骤进行操作: 1. ...
- OSGI引入Spring DM实现对服务对象的管理
一.异同 熟悉Spring的应该也都了解它的IOC的功能,那么对于在OSGI开发环境下,在使用IOC功能时有什么不同呢?最重要的一点就是Spring上下文对象,每个Spring-Powered Bun ...
- iOS.Crash.OniOS8.WhenCall[popToRootViewController]
系统iOS 8.x, ARC. CrashCase: 在UIViewController中有一个类型为UIScrollView的实例变量scrollView, 点击UIViewController中的 ...
- php中static静态关键字的使用方法和应用场景
php中除了常规类和方法的使用,访问控制之外,还有静态关键字static,静态变量可以是局部变量也可以是全局变量,当一个程序段执行完毕时,静态变量并没有消失,它依然存在于内存中,下次在定义时还是以前的 ...
- 厉害了,他用PS不是P照片而是……
今儿要介绍的主角是战斗民族的设计师 Dmitriy Glazyrin,他这个人用PS做设计有个特点,专门P3D软件做出来的白模. 大家可以想象一下,一个什么颜色什么材质都没有的东西,把它楞是用PS加上 ...
- include 指令和 include 动作引入 jsp 页面时中文乱码
include指令:<%@ include file="new.jsp" %> include动作:<jsp:include page="new.jsp ...
- 生活类App原型制作分享-AnyList
AnyList是一款可以帮你创建购物清单,并且帮助你整理食谱的生活工具App,前面引导页采用图片+文字的方式,介绍App的用法,登录注册采用选项卡切换的方式,减少了页面切换的繁琐操作,在Mockplu ...
- js模态框实现原理
<!DOCTYPE> <html> <head> <style>/* 定义模态对话框外面的覆盖层样式 */ #modal-overlay { visib ...
- spring boot 启动自动跳到 断点 throw new SilentExitException
项目 debug 启动,自动跳到 断点 ,而且就算F8 ,项目还是停止启动. 百度了一下,答案都是 Eclipse -> Preferences ->Java ->Debug去掉&q ...