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

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.l[i]记录在第i层中,从左往右数,最后一个“1”的位置; r[i]记录在第i层中,从右往左数,最后一个“1”的位置。

2.对于第i层楼的左梯,它可能是从下一层的左梯转移过来的,也可能是从下一层楼的右梯转移过来。对于右梯也一样。

所以:dp[i][j] 表示当到达第i层的j梯(0为左梯,1为右梯)时,所花费的最少步数。

状态转移方程:

{
dp[i][0] = min(dp[i-1][0]+2*l[i]+1, dp[i-1][1]+m+2);
dp[i][1] = min(dp[i-1][1]+2*r[i]+1, dp[i-1][0]+m+2);
}

代码如下:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const double eps = 1e-6;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int maxn = 15+10; int n, m, h;
int l[maxn], r[maxn], dp[maxn][2]; void init()
{
scanf("%d%d",&n,&m);
char s[150];
for(int i = n; i>=1; i--)
{
scanf("%s",s);
for(int j = 1; j<=strlen(s)-2; j++)
{
if(s[j]=='1')
{
if(!h) h = i;
if(!r[i]) r[i] = m+1-j;
l[i] = j;
}
}
}
} void solve()
{
dp[0][0] = 0;
dp[0][1] = INF/2;
for(int i = 1; i<h; i++)
{
dp[i][0] = min(dp[i-1][0]+2*l[i]+1, dp[i-1][1]+m+2);
dp[i][1] = min(dp[i-1][1]+2*r[i]+1, dp[i-1][0]+m+2);
}
int ans = min(dp[h-1][0]+l[h], dp[h-1][1]+r[h]);
cout<<ans<<endl;
} int main()
{
init();
solve();
}

Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister —— DP的更多相关文章

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

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

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

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

  3. 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 ...

  4. Codeforces Round #417 (Div. 2) D. Sagheer and Kindergarten(树中判祖先)

    http://codeforces.com/contest/812/problem/D 题意: 现在有n个孩子,m个玩具,每次输入x y,表示x孩子想要y玩具,如果y玩具没人玩,那么x就可以去玩,如果 ...

  5. Codeforces Round #417 (Div. 2)-A. Sagheer and Crossroad

    [题意概述] 在一个十字路口 ,给定红绿灯的情况, 按逆时针方向一次给出各个路口的左转,直行,右转,以及行人车道,判断汽车是否有可能撞到行人 [题目分析] 需要在逻辑上清晰,只需要把所有情况列出来即可 ...

  6. 【二分】Codeforces Round #417 (Div. 2) C. Sagheer and Nubian Market

    傻逼二分 #include<cstdio> #include<algorithm> using namespace std; typedef long long ll; ll ...

  7. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

  8. [Codeforces Round#417 Div.2]

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

  9. 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 ...

随机推荐

  1. Codeforces 622F The Sum of the k-th Powers

    Discription There are well-known formulas: , , . Also mathematicians found similar formulas for high ...

  2. C# 将 WebService 封装成动态库

    C# 将 WebService 封装成动态库 服务与服务之间的远程调用,经常会通过Web Service来实现,Web Service是支持跨语言调用的,可以是java调用c++或c#调用java等, ...

  3. Play框架连接Mysql遇到的一些问题

    最近,在基于Play框架的项目中需要连接Mysql数据库.在这个过程中遇到了一些问题.在此,把它记录下来. 首先,Play框架和Mysql连接有两种方式,这两种方式都是在application.con ...

  4. 邁向IT專家成功之路的三十則鐵律 鐵律二十六:IT人閱讀之道-慎選

    IT人經常一整天工作回來早已用腦過度,此時收看什麼樣的電視節目,以及閱讀甚麼樣的書籍.聽什麼樣的音樂與有聲書最適合我們,讓我們可以在放鬆之餘,還能夠讓自己內在的心靈與外在的能力繼續成長呢? 身為IT工 ...

  5. git/icode操作记录

    之前有一篇文章写了团队git的管理方式:link 今天处理了一个自动退款的脚本.提交到git.步骤如下: 1. 切换到本地master,update: git checkout master git ...

  6. VC2012编译protobuf出错处理

    近来要学习protobuf的协议生成.须要从网上下载它的代码,从这个SVN地址下载: 个.因此编译提示上面的出错.仅仅须要把std;;tuple里的个数定义为10个就可以.,因此不支持5个以上的參数输 ...

  7. C#遇见的函数

    1.类Stopwatch    提供一组方法和属性,可用于准确地测量运行时间. 命名空间:   System.Diagnostics Stopwatch timePerParse = Stopwatc ...

  8. NYOJ 38 布线问题_(解法1 Kruskal算法)

    时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描写叙述 南阳理工学院要进行用电线路改造.如今校长要求设计师设计出一种布线方式,该布线方式须要满足下面条件: 1.把全部的楼都供 ...

  9. 百度 BAE 项目部署

    转载:http://www.cnblogs.com/shamoyuu/p/node_bae.html 百度有一个应用引擎,价格非常便宜,Java的tomcat每天4毛钱,node每天2毛钱,我以前在上 ...

  10. Android——滑动事件冲突解决

    android中的事件类型分为按键事件和屏幕触摸事件,Touch事件是屏幕触摸事件的基础事件. android系统中的每个View的子类都具有下面三个与TouchEvent处理密切相关的方法: (1) ...