• Codeforces Round #417 B. Sagheer, the Hausmeister


    time limit per test  1 second
    memory limit per test  256 megabytes
     

    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 andm + 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.

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


    今天这题真是让人脑洞大开! (写之前先容我感慨一下)
    题目大意:
         这题说的是,给定一个n*(m+2)的)01矩阵,表示一栋n层的大楼每层m个房间第一列和最后一列全部是0代表这栋楼两边的楼梯
         1代表这个房间的灯是亮的 现在有一个人想要关掉所有的灯 他刚开始在(n,1)走到相邻的房间需要1秒,上楼需要一秒,当一个楼层
         中所有的灯都是关闭的时候,他直接去下一个楼层。最后不需要回到起点,问需要的最少时间?
    解题思路:
         不得不佩服网上的大佬们,当时让我束手无策的一题,大佬们竟然能用辣么多方法(变着花样的解出来) 、
         废话少说 下面我来记录一下我理解的一种思路
         主要思想的DP 定义一个dp数组dp[i][0]表示在第i层去i+1层的时候走的左边的楼梯,dp[i][1]表示走右边的楼梯
         每一层先找最左边亮灯的房间和最右边亮灯的房间,然后dp出走左右两个楼梯这两种情况的最优情况

         还有一点需要注意的是:要特殊判断一下每一层全是0的情况(全是0直接去下一层)

    AC代码:
     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <algorithm>
     4 using namespace std;
     5 
     6 int main ()
     7 {
     8     int n,m,i,j;
     9     char s[20][110];
    10     int left[110],right[110],dp[20][110];
    11     while (~scanf("%d%d",&n,&m))
    12     {
    13         memset(left,0,sizeof(left));
    14         memset(right,0,sizeof(right));
    15         memset(dp,0,sizeof(dp));
    16         
    17         for (i = n-1; i >= 0; i --){   // 找出每一层中最左和最右边亮灯的房间地址 
    18             scanf("%s",s[i]);
    19             for (j = 0; j < m+2; j ++){
    20                 if (s[i][j] == '1')
    21                     right[i] = j;
    22                 if (s[i][j] == '1' && left[i] == 0)
    23                     left[i] = j;
    24             }
    25         }
    26         for(i = n-1; i >= 0; i --){
    27             if (left[i]==0 && right[i]==0)
    28                 n --;
    29             else
    30                 break;
    31         }
    32         if (n == 0){     // 没有亮灯的房间 
    33             printf("0
    ");
    34             continue;
    35         }
    36         else if(n == 1){   // 只有第一层有亮灯的房间 
    37             printf("%d
    ",right[0]);
    38         }
    39         else{
    40             dp[0][0] = 2*right[0]+1;  // 初始化第一层所花费的时间 
    41             dp[0][1] = m+2;
    42             for (i = 1; i < n-1; i ++){
    43                 if (left[i]==0 && right[i]==0){   // 该层所有房间的灯全是灭的 
    44                     dp[i][0] = dp[i-1][0]+1;
    45                     dp[i][1] = dp[i-1][1]+1;
    46                 }
    47                 else{
    48                     dp[i][0] = min(dp[i-1][0]+2*right[i],dp[i-1][1]+m+1)+1;
    49                     dp[i][1] = min(dp[i-1][0]+m+1,dp[i-1][1]+2*(m+1-left[i]))+1;
    50                 }
    51             }
    52             printf("%d
    ",min(dp[n-2][0]+right[n-1],dp[n-2][1]+m+1-left[n-1]));
    53         }
    54         
    55     }
    56     return 0;
    57 }

      向大佬看齐! 刷题! 刷题!! 刷题!!!

  • 相关阅读:
    DB数据导出工具分享
    使用批处理脚本愉快的清理缓存
    git常用命令记录
    使用bat脚本部署hexo到coding和github
    初次尝试Linux并记录一二
    js实用方法记录-指不定哪天就会用到的js方法
    js实用方法记录-简单cookie操作
    js实用方法记录-js动态加载css、js脚本文件
    使用node自动生成html并调用cmd命令提交代码到仓库
    express使用记录
  • 原文地址:https://www.cnblogs.com/yoke/p/6994542.html
Copyright © 2020-2023  润新知