2018-BNUZ-ACM-GDCPC选拔赛 CodeForces 812B【补】

原创 2018年04月16日 22:28:43
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.

题意:有个人要关一个建筑所有的灯,输入一幅地图,从左下开始跑,1表示灯开的,0表示灯是关的,建筑有左右两边的楼梯。初始时在左楼梯,

这一层的灯没有关之前 不能上楼,走一个格子用1步,求关掉所有灯用的最少步数。

题解:DP,dp[i][j],i 表示 楼层数,j 表示 左右楼梯,dp[i][j] 表示 走到此楼梯所用的最少步数(把本层的灯关完后), 这题记得考虑 有可能一层楼中没有灯是亮的的情况。 当前楼梯可能是从 下一层左楼梯和右楼梯 上来 ,对他们取最小值来维护。

AC代码:

#include <bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f

using namespace std;

int n, m, dp[20][2], l[20], r[20];
string mapp[20];

int main() {
	mem(dp, INF);
	mem(l, 0);
	mem(r, 0);
	cin >> n >> m;
	for (int i = n-1; i >= 0; i--) {
		cin >> mapp[i];
		for (int j = 0; j < m+2; j++) {
			if (mapp[i][j] == '1') {
				l[i] = j;
				break;
			}
		}
		for (int j = m+1; j >= 0; j--) {
			if (mapp[i][j] == '1') {
				r[i] = j;
				break;
			}
		}
	}
//	
//	for (int i = 0; i < n; i++) {
//		cout << l[i] << " " << r[i] << endl;
//	}
	for (int i = n-1; i >= 0; i--) { //获取有亮灯的最上层 
		if (!l[i] && !r[i])
			n--;
		else
			break;
	}
	if (n == 1) {
		cout << r[0] << endl;
		return 0;
	}
	dp[0][0] = 2 * r[0] + 1;
	dp[0][1] = m + 2;
	for (int i = 1; i < n - 1; i++) {
		if (!l[i] && !r[i]) {
			dp[i][0] = dp[i-1][0] + 1;
			dp[i][1] = dp[i-1][1] + 1;
		} else {
			dp[i][0] = min(dp[i-1][0] + 2 * r[i], dp[i-1][1] + m + 1) + 1;
			dp[i][1] = min(dp[i-1][0] + m + 1, dp[i-1][1] + 2 * (m+1 - l[i])) + 1;
		}
	}
//	cout << "--------" <<endl;
//	for (int i = 0; i < n-1; i++) {
//		for (int j = 0; j < 2; j++) {
//			printf("%d ", dp[i][j]);
//		}
//		cout << endl;
//	}
//	cout << "L[0]=" << l[0] << " R[0]=" << r[0] << endl;
//	cout << "R[n-1]=" << r[n-1] << " L[n-1]=" << l[n-1] << endl;
	cout << min(dp[n-2][0] + r[n-1], dp[n-2][1] + (m+1 - l[n-1])) << endl;
	return 0;
}



2018-BNUZ-ACM-GDCPC选拔赛 CodeForces 766A【补】

A. Mahmoud and Longest Uncommon Subsequencetime limit per test2 secondsmemory limit per test256 mega...
  • qq_40731186
  • qq_40731186
  • 2018-04-16 12:45:40
  • 12

2018-BNUZ-ACM-GDCPC选拔赛 CodeForces 766B【补】

B. Mahmoud and a Triangletime limit per test2 secondsmemory limit per test256 megabytesinputstandard...
  • qq_40731186
  • qq_40731186
  • 2018-04-16 12:31:56
  • 1

2018-BNUZ-ACM-GDCPC选拔赛 CodeForces 812C【补】

C. Sagheer and Nubian Markettime limit per test2 secondsmemory limit per test256 megabytesinputstand...
  • qq_40731186
  • qq_40731186
  • 2018-04-16 16:43:16
  • 8

Codeforces812B Sagheer, the Hausmeister

B. Sagheer, the Hausmeister time limit per test 1 second memory limit per test 256 megab...
  • Jelly_acm
  • Jelly_acm
  • 2017-06-02 20:47:56
  • 302

Codeforces 812B-Sagheer, the Hausmeister

Sagheer, the Hausmeister time limit per test 1 second memory limit per test 256 megabyte...
  • a664607530
  • a664607530
  • 2017-06-02 10:43:33
  • 694

CodeForces - 812B

Some people leave the lights at their workplaces on when they leave that is a waste of resources. As...
  • NineFailure
  • NineFailure
  • 2017-06-03 22:58:16
  • 353

Sagheer, the Hausmeister CodeForces - 812B dp

题目链接 思路: 只说一下这个题dp的思想吧,我们对于每一行都记录最左端未关灯的位置,和最右端未关灯的位置,那么对于左端和右端的楼梯维护一下是将本层楼灯全部关闭原路返回花的时间少,还是从右...
  • HowardEmily
  • HowardEmily
  • 2017-06-03 17:49:15
  • 288

CodeForces - 812B 补 题解

这道题一开始以为是有规律的的, 想通过最大区域的0来计算,然后用bfs来解决,但是写到一半又发现思路错了,最后还是学长提醒我用dp思路去做,纪录最右和最左的点,dp已经很久没做了,看了网上其他题解才明...
  • anthony1314
  • anthony1314
  • 2018-04-16 19:28:14
  • 2

2018-BNUZ-ACM-GDCPC选拔赛 【补题】

第一次GDCPC选拔,说不想去是不可能的,中午回来就赶紧休息,下午又早早的来到教室,可是还是自己的问题吧,又是卡题,就是一点思路都没有,就想着暴力,结果过不去各种wa还有tle,第一次出校打比赛的机会...
  • YIOWOIY
  • YIOWOIY
  • 2018-04-16 00:58:10
  • 7

codeforces 812B

题目链接:点击打开链接题意:输入n,m,表示n行m间房间,0为关灯房间(左右两边的0为楼梯),1为亮灯房间,管理员从左下角开始关灯,每一层房间灯关完才能进入下一层,每次移动花费一分钟;求关完所有灯需花...
  • LCCCC233
  • LCCCC233
  • 2018-03-17 21:20:28
  • 13
收藏助手
不良信息举报
您举报文章:2018-BNUZ-ACM-GDCPC选拔赛 CodeForces 812B【补】
举报原因:
原因补充:

(最多只允许输入30个字)