Codeforces 812B-Sagheer, the Hausmeister

梁丘霖
2023-12-01

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.


题意:一栋楼关灯,nm,n是多少层,m是一层多少间屋,其中给n行数据,每行数据m+2个点,1代表灯亮着,0代表灯灭着,其中第一个和最后一个点是楼梯,起点在左下角,走一个格时间是1,只能通过楼梯上楼,并且必须关掉当前层的所有灯之后才能上楼,问关掉所有的灯需要的最少时间

解题思路:DP,每一层的左楼梯和右楼梯都有两个选择策略,左楼梯可以是下面层的右楼梯走过来的,也可以是下面层的左楼梯先去关灯在走回来的,右楼梯一样。在特殊考虑一下相当只有一层的


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <bitset>
#include <set>
#include <vector>
#include <functional>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

int n, m;
char ch[20][120];
int l[20], r[20],visit[20],dp[20][2];

int main()
{
	while (~scanf("%d%d", &n, &m))
	{
		memset(visit, 0, sizeof visit);
		memset(r,0,sizeof r);
		memset(l,0,sizeof l);
		for (int i = n; i >= 1; i--)
		{
			scanf("%s", ch[i] + 1);
			for (int j = 2; j <= m + 1; j++)
				if (ch[i][j] == '1') { l[i] = j;visit[i] = 1;break; }
			for (int j = m + 1; j >= 2; j--)
				if (ch[i][j] == '1') { r[i] = j; break; }
		}
		int x=n;
        while(!visit[x--]&&x>=1) n--;
		if(n==1) {printf("%d\n",r[1]?r[1]-1:0);continue;}
		memset(dp,INF,sizeof dp);
		dp[1][0]=r[1]?(r[1]-1)*2:0,dp[1][1]=m+1;
		for(int i=2;i<n;i++)
        {
            if(!visit[i])
            {
                dp[i][0]=dp[i-1][0]+1;
                dp[i][1]=dp[i-1][1]+1;
                continue;
            }
            dp[i][0]=min(dp[i-1][0]+2*(r[i]-1)+1,dp[i][0]);
            dp[i][0]=min(dp[i-1][1]+m+1+1,dp[i][0]);
            dp[i][1]=min(dp[i-1][1]+2*(m+2-l[i])+1,dp[i][1]);
            dp[i][1]=min(dp[i-1][0]+m+1+1,dp[i][1]);
        }
        int ans=min(dp[n-1][0]+r[n],dp[n-1][1]+1+m+2-l[n]);
        printf("%d\n",ans);
	}
	return 0;
}

 类似资料:

相关阅读

相关文章

相关问答