codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

812B.md (3045B)


      1 Source: https://codeforces.com/problemset/problem/812/B
      2 
      3 B. Sagheer, the Hausmeister
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 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.
     22 
     23 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.
     24 
     25 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.
     26 
     27 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.
     28 
     29 Input
     30 
     31 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.
     32 
     33 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.
     34 
     35 The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.
     36 
     37 Output
     38 
     39 Print a single integer — the minimum total time needed to turn off all the lights.
     40 
     41 Examples
     42 
     43 Input
     44 
     45 ```
     46 2 2  
     47 0010  
     48 0100
     49 ```
     50 
     51 Output
     52 
     53 ```
     54 5
     55 ```
     56 
     57 Input
     58 
     59 ```
     60 3 4  
     61 001000  
     62 000010  
     63 000010
     64 ```
     65 
     66 Output
     67 
     68 ```
     69 12
     70 ```
     71 
     72 Input
     73 
     74 ```
     75 4 3  
     76 01110  
     77 01110  
     78 01110  
     79 01110
     80 ```
     81 
     82 Output
     83 
     84 ```
     85 18
     86 ```
     87 
     88 Note
     89 
     90 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.
     91 
     92 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.
     93 
     94 In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor.