有些人下班时把工作场所的灯开着,这是一种资源浪费。作为DHBW的校长,Sagheer等到所有的学生和教授都离开了教学楼,然后走过去把所有的灯都关了。
建筑由n层楼组成,左右两侧有楼梯。每层楼有m个房间在同一条线上,走廊连接左右楼梯,通过所有的房间。换句话说,该建筑可以表示为一个有n行和m+2列的矩形,其中第一列和最后一列表示楼梯,中间的m列表示房间。
Sagheer站在一楼左边的楼梯上。他想把所有的灯都关掉,这样他就不会上楼,直到他站的那层楼所有的灯都关掉。当然,Sagheer必须去一个房间把灯关掉。Sagheer使用楼梯到达下一层或从当前房间/楼梯移动到同一层的相邻房间/楼梯需要1分钟。在他所站的房间里,他很快就把灯关掉了。帮助Sagheer找到关掉所有灯的最短总时间。
请注意,Sagheer不必回到他的起始位置,他也不必去已经关灯的房间。
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.
2 2 0010 0100
5
3 4 001000 000010 000010
12
4 3 01110 01110 01110 01110
18
第一行包含两个整数n和m(1≤n≤15和1≤m≤100) − 楼层数和每个楼层中的房间数。
接下来的n行包含建筑描述。每一行包含一个长度为m+2的二进制字符串,表示一个楼层(左楼梯,然后是m个房间,然后是右楼梯),其中0表示灯关闭,1表示灯打开。楼层从上到下列出,因此最后一行表示底层。
每个字符串的第一个和最后一个字符分别表示左楼梯和右楼梯,因此它们始终为0。
打印单个整数− 关闭所有灯所需的最小总时间。
Examples
2 2 0010 0100
5
3 4 001000 000010 000010
12
4 3 01110 01110 01110 01110
18
2 2
0010
0100
5
在第一个例子中,Sagheer会去一楼的1号房间,然后他会使用左右楼梯去二楼的2号房间。
在第二个例子中,他会去一楼的第四个房间,走右边的楼梯,然后去二楼的第四个房间,再走右边的楼梯,然后去最后一层的第二个房间。
在第三个例子中,他将在每层左右楼梯之间交替穿过整个走廊。