812B(状态压缩)
来源:互联网 发布:淘宝评价后不计分 编辑:程序博客网 时间:2024/06/04 01:14
B. Sagheer, the Hausmeister
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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.
解题思路:每次关完当前楼的所有灯之后,有两个选择,从左边上楼或者从右边上楼,N最大15,我们们可以枚举在每一楼向左或者向右,一共就2^15次方种选择,然后取最优的那个就行
#include<bits/stdc++.h>using namespace std;const int maxn = 200;int n, m;char Matrix[maxn][maxn];int l[maxn],lc[maxn];int r[maxn],rc[maxn];int main(){ while(~scanf("%d%d", &n, &m)) { for(int i = 1; i <= n; i++) { scanf("%s", Matrix[i] + 1); lc[i] = 1; for(int j = 2; j <= m + 1; j++) { if(Matrix[i][j] == '1') lc[i] = j; } l[i] = lc[i] - 1; rc[i] = m + 2; for(int j = m + 1; j >= 2; j--) { if(Matrix[i][j] == '1') rc[i] = j; } r[i] = m + 2 - rc[i]; } int flag = -1; for(int i = 1; i <= n; i++) { for(int j = 1; j <= m + 2; j++) { if(Matrix[i][j] == '1') { flag = i; break; } } if(flag != -1) break; } if(flag == -1) { printf("0\n"); continue; } int ans = 0; int loc = 1; for(int i = 2; i <= m + 1; i++) { if(Matrix[n][i] == '1') loc = i; } ans += loc - 1; int d = n - 1 - flag + 1; if(d == 0) { printf("%d\n", ans); continue; } int Min = 1e8; int x = 0; for(int i = 0; i < d; i++) { x += (1<<i); } for(int i = 0; i <= x; i++) { int term = i; int res = loc; int sum = 0; for(int j = n - 1; j >= flag; j--) { int num = term&1; term >>= 1; if(num == 1)//左 { sum += res - 1; sum++; sum += l[j]; res = lc[j]; } else { sum += m + 2 - res; sum++; sum += r[j]; res = rc[j]; } } Min = min(Min, sum); } printf("%d\n", ans + Min); } return 0;}
- 812B(状态压缩)
- CF 453B状态压缩dp
- Codeforces Round #306 (Div. 2)B. Preparing Olympiad--状态压缩
- CodeForces 550B Preparing Olympiad(状态压缩,暴搜)
- Codeforces Round #306 (Div. 2) B. Preparing Olympiad (状态压缩)
- 状态压缩
- 状态压缩
- 状态压缩
- 状态压缩
- 状态压缩
- 状态压缩
- 状态压缩
- 状态压缩
- 状态压缩
- 状态压缩
- 状态压缩
- 状态压缩
- 状态压缩
- Unity3D Hololens 真机调试 在VS2015上调试应用程序
- HLS使用指南
- Retrofit A type-safe HTTP client for Android and Java
- 正则笔记-贪婪,非贪婪模式。
- 在web.xml中配置error-page
- 812B(状态压缩)
- 浙江工业大学之江学院程序设计竞赛决赛
- [Leetcode]Test1 TwoSum [java]题目分析解答
- Unity3D研究院之两种方式播放游戏视频(本文转载自 http://www.xuanyusong.com/archives/1019 )
- Laravel Excel导出xls乱码
- 免费开源的电路图和PCB绘图软件KiCAD
- Android开发技巧——写一个StepView
- javamail实现QQ发送邮件
- 关于.net mvc中@Html.DropDownListFor和@Html.DropDownList默认值无法选中问题