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.

每次关完当前楼层的灯以后都有两种操作,向左上去或者向右上去,暴力dfs即可,注意一些边界条件,如

当全部的灯都关闭后不必再上楼,必须关闭所有当前楼层的灯后再上楼,

#include<bits/stdc++.h>
using namespace std;
#define LL long long
int e[30][105],b[20][20];
int total[30];
int n,m,s=0;
int dfs(int cur,int st,int sumn,int dic)
{
  if(st==s) return sumn;
  else{
   if(b[cur][0]==0){        //empty  floor
       if(dic==0){
          int s1=dfs(cur+1,st,sumn+1,dic);
          int s2=dfs(cur+1,st,sumn+m,1);
          return s1<s2?s1:s2;
         }
       else if(dic==1){
            int s1=dfs(cur+1,st,sumn+1,dic);
            int s2=dfs(cur+1,st,sumn+m,0);
            return s1<s2?s1:s2;
         }
   }

else{int num=total[cur];
       if(dic==0){
          if(st+num==s){
            return sumn+b[cur][1]-1;
          }
          else{
            int tmp=b[cur][1]-1;
            int s1=dfs(cur+1,st+num,sumn+tmp+tmp+1,0);
            int s2=dfs(cur+1,st+num,sumn+tmp+(m-b[cur][1]+1),1);
            return s1<s2?s1:s2;
          }
       }
       else if(dic==1){
           if(st+num==s){
            return sumn+m-b[cur][0];
          }
          else{
            int tmp=m-b[cur][0];
            int s1=dfs(cur+1,st+num,sumn+tmp+tmp+1,1);
            int s2=dfs(cur+1,st+num,sumn+tmp+b[cur][0],0);
            return s1<s2?s1:s2;
          }
       }
   }
  }
}

int main()
{
    int i,j,k;
    cin>>n>>m;
    m+=2;
    for(i=1;i<=n;++i)
    for(j=1;j<=m;++j){
        char ch;
        cin>>ch;
        e[i][j]=ch-'0';
        s+=e[i][j];
    }
    for(i=n,k=1;i>=1;--i,++k){ int jud=0;
               for(j=1;j<=m;++j){
                 if(e[i][j]==1){
                    if(jud==0) b[k][0]=b[k][1]=j;
                    else b[k][1]=j;
                    jud++;
                 }
               }
               total[k]=jud;
    }
    cout<<dfs(1,0,0,0)<<endl;
    return 0;
}

cf812B 搜索的更多相关文章

  1. SQLSERVER走起微信公众帐号已经开通搜狗微信搜索

    SQLSERVER走起微信公众帐号已经开通搜狗微信搜索 请打开下面链接 http://weixin.sogou.com/gzh?openid=oIWsFt-hiIb_oYqQHaBMoNwRB2wM ...

  2. solr_架构案例【京东站内搜索】(附程序源代码)

    注意事项:首先要保证部署solr服务的Tomcat容器和检索solr服务中数据的Tomcat容器,它们的端口号不能发生冲突,否则web程序是不可能运行起来的. 一:solr服务的端口号.我这里的sol ...

  3. SQLServer地址搜索性能优化例子

    这是一个很久以前的例子,现在在整理资料时无意发现,就拿出来再改写分享. 1.需求 1.1 基本需求: 根据输入的地址关键字,搜索出完整的地址路径,耗时要控制在几十毫秒内. 1.2 数据库地址表结构和数 ...

  4. HTML5轻松实现搜索框提示文字点击消失---及placeholder颜色的设置

    在做搜索框的时候无意间发现html5的input里有个placeholder属性能轻松实现提示文字点击消失功能,之前还傻傻的在用js来实现类似功能... 示例 <form action=&quo ...

  5. bzoj1079--记忆化搜索

    题目大意:有n个木块排成一行,从左到右依次编号为1~n.你有k种颜色的油漆,其中第i种颜色的油漆足够涂ci个木块.所有油漆刚好足够涂满所有木块,即c1+c2+...+ck=n.相邻两个木块涂相同色显得 ...

  6. bzoj3208--记忆化搜索

    题目大意: 花花山峰峦起伏,峰顶常年被雪,Memphis打算帮花花山风景区的人员开发一个滑雪项目.    我们可以把风景区看作一个n*n的地图,每个点有它的初始高度,滑雪只能从高处往低处滑[严格大于] ...

  7. Android中通过ActionBar为标题栏添加搜索以及分享视窗

    在Android3.0之后,Google对UI导航设计上进行了一系列的改革,其中有一个非常好用的新功能就是引入的ActionBar,他用于取代3.0之前的标题栏,并提供更为丰富的导航效果.Action ...

  8. 一步步开发自己的博客 .NET版(5、Lucenne.Net 和 必应站内搜索)

    前言 这次开发的博客主要功能或特点:    第一:可以兼容各终端,特别是手机端.    第二:到时会用到大量html5,炫啊.    第三:导入博客园的精华文章,并做分类.(不要封我)    第四:做 ...

  9. Go语言实战 - 我需要站内搜索

    山坡网的用户抱怨"为什么搜索'二鬼子李富贵'找不到'二鬼子汉奸李富贵'?我用百度搜都能找到." 当时我就滴汗了,用户说的有道理,应该要能搜索到. 之前的方案很简单,用户输入的字串会 ...

随机推荐

  1. ora-28056错误解决

    问题描述:今天有同事找我,说是oracle数据库的监听器出现问题,我连接服务器查看后,发现不是监听器问题,而是进程连接数已经达到150个了,客户端连接不上服务器,因其是测试服务器,重启服务器后再次sq ...

  2. nginx 上php不可写解决方法

    在php.ini中设置的session.save_path会被php-fpm.conf中覆盖 打开php-fpm.conf文件找到php_value['session.save_apth'] 这里的/ ...

  3. Crossed ladders---poj2507(二分+简单几何)

    题目链接:http://poj.org/problem?id=2507   题意就是给你x y c求出?的距离: h1 = sqrt(x*x-d*d); h2 = sqrt(y*y-d*d); (h1 ...

  4. 广通软件荣获“2016年度ITSS优秀会员”称号

    1月12日,为了表彰在IT服务标准研制和应用推广工作中所做出的贡献,中国电子工业标准化技术协会信息技术服务分会(以下称ITSS分会)在北京召开“2016年度ITSS优秀会员”专家评选活动,广通软件获得 ...

  5. SNMP 原理及配置简述 net-snmp-utils net-snmp 第2版基于SNMP 群体名(community name) 第3版引入了安全性更高的访问控制方法 SNMP协议操作只有4种 Apache的php_snmp 模块

    SNMP 原理及配置简述  net-snmp-utils  net-snmp 第2版基于SNMP 群体名(community name) 第3版引入了安全性更高的访问控制方法 SNMP协议操作只有4种 ...

  6. HDU3579:Hello Kiki(解一元线性同余方程组)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3579 题目解析:求一元线性同余方程组的最小解X,需要注意的是如果X等于0,需要加上方程组通解的整数区间lc ...

  7. PAT 1102 Invert a Binary Tree[比较简单]

    1102 Invert a Binary Tree(25 分) The following is from Max Howell @twitter: Google: 90% of our engine ...

  8. [C#]解决程序Vista/Win7下因UAC导致的读写错误

    在微软的操作系统中,vista和win7加入了UAC的功能,UAC(User Account Control,用户帐户控制)是微软为提高系统安全而在Windows Vista中引入的新技术,它要求用户 ...

  9. #C++初学记录(并查集)

    并查集 题目 今天是伊格那丢的生日.他邀请了很多朋友.现在该吃晚饭了.伊格那丢想知道他至少需要多少张桌子.你必须注意到并不是所有的朋友都认识对方,而且所有的朋友都不想和陌生人待在一起.这个问题的一个重 ...

  10. 53. Maximum Subarray(动态规划 求最大子数组)

      Find the contiguous subarray within an array (containing at least one number) which has the larges ...