预处理每一层最左侧的1的位置,以及最右侧的1的位置。

f(i,0)表示第i层,从左侧上来的最小值。f(i,1)表示从右侧上来。

转移方程请看代码。

#include<cstdio>
#include<algorithm>
using namespace std;
int n,m,left[20],right[20],f[20][2];
char a[20][110];
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i){
scanf("%s",a[i]+1);
}
for(int i=1;i<=n;++i){
left[i]=m+2;
right[i]=1;
for(int j=1;j<=m;++j){
if(a[i][j+1]=='1'){
left[i]=j+1;
break;
}
}
for(int j=m;j>=1;--j){
if(a[i][j+1]=='1'){
right[i]=j+1;
break;
}
}
}
f[n][1]=m+1;
for(int i=n-1;i>=0;--i){
f[i][0]=min(f[i+1][0]+(right[i+1]-1)*2+1,f[i+1][1]+m+2);
f[i][1]=min(f[i+1][1]+(m+2-left[i+1])*2+1,f[i+1][0]+m+2);
}
int nn=n+1; left[nn]=m+2; right[nn]=1;
for(int i=n;i>=1;--i){
if(right[i]!=1){
nn=i;
}
}
printf("%d\n",min(f[nn][0]+right[nn]-1,f[nn][1]+m+2-left[nn]));
return 0;
}

【动态规划】Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister的更多相关文章

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

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

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

  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#417 Div.2]

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

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

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

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

随机推荐

  1. Java多线程学习(四)等待/通知(wait/notify)机制

    转载请备注地址:https://blog.csdn.net/qq_34337272/article/details/79690279 系列文章传送门: Java多线程学习(一)Java多线程入门 Ja ...

  2. IBM InfoSphere DataStage and QualityStage

    Info coms from https://www.ibm.com/support/knowledgecenter/en/SSZJPZ_9.1.0/com.ibm.swg.im.iis.ds.nav ...

  3. %和format 细说

    Python中格式化字符串目前有两种阵营:%和format,我们应该选择哪种呢? 自从Python2.6引入了format这个格式化字符串的方法之后,我认为%还是format这根本就不算个问题.不信你 ...

  4. chrome://settings/content

    chrome://settings/content C:\Users\用户名\AppData\Roaming\Microsoft\Internet Explorer chrome://version/

  5. device tree property ---- interrupt-names

    device tree source 的 interrupt-names property 會對應到 pltform_get_irq_byname() 的第二個參數. .dtsi or .dts in ...

  6. MNIST数据集转化为二维图片

    #coding: utf-8 from tensorflow.examples.tutorials.mnist import input_data import scipy.misc import o ...

  7. HTML5API(5)

    一.SVG 1.svg与canvas的区别 canvas绘制的是位图,svg绘制的是矢量图 canvas使用JavaScript绘制,svg使用xml绘制 canvas不能给每个图形绑定事件,svg可 ...

  8. PHP常用函数总结(180多个)

    PHP常用函数总结 数学函数 1.abs(): 求绝对值 $abs = abs(-4.2); //4.2 数字绝对值数字 2.ceil(): 进一法取整 echo ceil(9.999); // 10 ...

  9. leetcode 之Reorder List(25)

    找到中间结点,将后半部分反转接入即可. ListNode *reoderList(ListNode* head) { if (head == nullptr || head->next == n ...

  10. Django web框架之权限管理二

    1. login登录 def login(request): if request.method=="GET": return render(request,'login.html ...