zoukankan      html  css  js  c++  java
  • cf812B 搜索

    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;
    }

  • 相关阅读:
    Asp.MVC 各个版本比较(资源整合)与WebForm的区别
    20款最新且极具创意的jQuery插件(附下载)
    php java net 开发比较
    移动互联网渠道乱象
    基于微软IIS/.NET平台开发的知名网站 (补充)
    sqlsql语句查询优化总结,建议及写法技巧(汇总)
    总结关于对日外包的一些想法
    .net跨平台解决方案mono真正实现C#代码一次编写处处运行(微软已经正式支持夸平台框架aspnet core)
    .NET 常用经典学习资源网站推荐
    常用visual studio 插件工具
  • 原文地址:https://www.cnblogs.com/zzqc/p/6932650.html
Copyright © 2011-2022 走看看