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.

贪心+模拟

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define ios() ios::sync_with_stdio(false)
#define INF 0x3f3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
char a[][];
int n,m,ans,pos,f[][],pl=,pr=MOD;
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%s",a[n-i+]+);
int k=;
for(int i=;i<=n;i++)
for(int j=;j<=m+;j++)
if(a[i][j]=='') k=i;
for(int i=;i<=k;i++)
{
int l=,r=m+;
for(int j=;j<=m+;j++) if(a[i][j]=='') l=j;
for(int j=m+;j>=;j--) if(a[i][j]=='') r=j;
ans=pl,pos=pr;
if(i<k)
{
pl=min(ans+(l-)*,pos+m+);//返回加一路走到黑
pr=min(pos+(m+-r)*,ans+m+);
}
else
{
pl+=l-;
pr+=m+-r;
}
}
printf("%d\n",min(pl,pr)+k-);
return ;
}

Codefroces 812 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. Codeforces812B Sagheer, the Hausmeister 2017-06-02 20:47 85人阅读 评论(0) 收藏

    B. Sagheer, the Hausmeister time limit per test 1 second memory limit per test 256 megabytes input s ...

  3. Codeforces Round #417 B. Sagheer, the Hausmeister

    B. Sagheer, the Hausmeister time limit per test  1 second memory limit per test  256 megabytes   Som ...

  4. AC日记——Sagheer, the Hausmeister codeforces 812b

    812B - Sagheer, the Hausmeister 思路: 搜索: 代码: #include <cstdio> #include <cstring> #includ ...

  5. CodeForce-812B Sagheer, the Hausmeister(DFS)

    Sagheer, the Hausmeister CodeForces - 812B 题意:有一栋楼房,里面有很多盏灯没关,为了节约用电小L决定把这些灯都关了. 这楼有 n 层,最左边和最右边有楼梯. ...

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

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

  7. 【DFS】codeforces B. Sagheer, the Hausmeister

    http://codeforces.com/contest/812/problem/B [题意] 有一个n*m的棋盘,每个小格子有0或1两种状态,现在要把所有的1都变成0,问最少的步数是多少?初始位置 ...

  8. 【codeforces 812B】Sagheer, the Hausmeister

    [题目链接]:http://codeforces.com/contest/812/problem/B [题意] 一个老大爷在一楼; 然后他有n楼的灯要关(最多n楼); 每楼有m个房间; 给出每个房间的 ...

  9. codeforces 812 E. Sagheer and Apple Tree(树+尼姆博弈)

    题目链接:http://codeforces.com/contest/812/problem/E 题意:有一颗苹果树,这个苹果树所有叶子节点的深度要不全是奇数,要不全是偶数,并且包括根在内的所有节点上 ...

随机推荐

  1. ArcGIS 空间查询

    public static bool QueryMessPoint(IActiveView activeView, IFeatureClass featureClass, string whereCl ...

  2. modSecurity规则学习(八)——防止CC攻击

    modSecurity日志收集:在phase 5阶段处理. 由于CC攻击主要考虑对动态请求的防护,所以要排除静态资源的请求,或者自定义动态请求的后缀或者关键字做接口针对性的防护. 定义需要排除的请求u ...

  3. Android TextureView简易教程

    如果你想显示一段在线视频或者任意的数据流比如视频或者OpenGL 场景,你可以用android中的TextureView做到. TextureView的兄弟SurfaceView 应用程序的视频或者o ...

  4. 判断DataGridView滚动条是否滚动到当前已加载的数据行底部

    private void dataGridView1_Scroll(object sender, ScrollEventArgs e) {   if (e.ScrollOrientation == S ...

  5. js数组与字符串的相互转换方法 数组常用的方法

    1 数组转字符串 需要将数组元素用某个字符连接成字符串,示例代码如下: var a, b; a = new Array(0,1,2,3,4); b = a.join("-"); 二 ...

  6. 【Docker构建私有仓库】

    Docker默认不允许非HTTPS方式推送镜像,我们可以通过Docker的配置选项来取消此限制: [root@fedora ~]# cat /etc/docker/daemon.json { &quo ...

  7. vue踩坑-This dependency was not found

    * vux in ./node_modules/babel-loader/lib!./node_modules/vue-loader/lib/selector.js?type=script&i ...

  8. UVALive 6869 Repeated Substrings

    Repeated Substrings Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Descri ...

  9. C# async/await异步编程深入理解

    异步函数简介 一般指 async 修饰符声明得.可包含await表达式得方法或匿名函数. 声明方式 异步方法的声明语法与其他方法完全一样, 只是需要包含 async 关键字.async可以出现在返回值 ...

  10. 欢天喜地迎国庆,国产开源编程语言 RPP 1.87 公布!

    更新例如以下: 1.支持超级宏 2.修复bug 下载地址: https://github.com/roundsheep/rpp 超级宏能够随意定义语法,制约你的仅仅有想象力: void main() ...