CodeForces - 812B Sagheer, the Hausmeister 搜索 dp
题意:给你n行长度为m的01串(n<15,m<100) 。每次只能走一步,要将所有的1变为零,问最少的步数,注意从左下角开始,每次要将一层清完才能走到上一层,每次只有在第一列或者最后一列才能往上走一层,否则只能左右移动。
题解:由于清完当前层才能继续上一层,所以必然存在一个递推关系。先递推预处理,然后输出ans即可。用far来存最远的那个1,如果这层没有1,假设有一个,令其距离为1
具体看代码注释吧。
坑:没考虑没有1以及只有1层的情况
#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<string>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + ;
int m, n, len;
int dp[][];//清完i层回到最左0右1 所需的min
int far[][];//每层楼离左0右1 最远的灯
int no[];
int main() {
while (cin >> n >> m) {
for (int i = ; i <= n; i++) {//从顶层开始输入
string s;
cin >> s;
len = s.length();
int j;
int ok = ;
for (j = ; j < len - ; j++) if (s[j] == '') { far[n + - i][] = len - j; break; } for (j = len - ; j > ; j--)if (s[j] == '') { far[n + - i][] = j + ; break; }
for (j = ; j < len; j++) { if (s[j] == '') ok = ; }
if (ok == )far[n + - i][] = , far[n + - i][] = , no[n + - i] = ;
}
//如果某层没灯+1跳过:
if (no[]) far[][] = ;//floor 1 no lamp
dp[][] = len - ; dp[][] = far[][] * - ;//wa这儿了,忘记考虑只有1层,应该改far
for (int i = ; i <= n; i++) {
for (int j = ; j <= ; j++)dp[i][j] = maxn;
dp[i][] = min(max(, dp[i - ][] + far[i][] * - ), dp[i - ][] + len);
dp[i][] = min(max(, dp[i - ][] + far[i][] * - ), dp[i - ][] + len);
} //1st floor start from left,
int k = n;
while (no[k])k--; int ans = min(dp[k - ][] + far[k][], dp[k - ][] + far[k][]);
//1层
if (k == ) {
far[][]--; ans = far[][];
}
cout << ans << endl;
}
cin >> n;
}
CodeForces - 812B Sagheer, the Hausmeister 搜索 dp的更多相关文章
- 【DFS】codeforces B. Sagheer, the Hausmeister
http://codeforces.com/contest/812/problem/B [题意] 有一个n*m的棋盘,每个小格子有0或1两种状态,现在要把所有的1都变成0,问最少的步数是多少?初始位置 ...
- AC日记——Sagheer, the Hausmeister codeforces 812b
812B - Sagheer, the Hausmeister 思路: 搜索: 代码: #include <cstdio> #include <cstring> #includ ...
- Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister —— DP
题目链接:http://codeforces.com/problemset/problem/812/B B. Sagheer, the Hausmeister time limit per test ...
- Codeforces Round #417 B. Sagheer, the Hausmeister
B. Sagheer, the Hausmeister time limit per test 1 second memory limit per test 256 megabytes Som ...
- CodeForce-812B Sagheer, the Hausmeister(DFS)
Sagheer, the Hausmeister CodeForces - 812B 题意:有一栋楼房,里面有很多盏灯没关,为了节约用电小L决定把这些灯都关了. 这楼有 n 层,最左边和最右边有楼梯. ...
- Codeforces812B Sagheer, the Hausmeister 2017-06-02 20:47 85人阅读 评论(0) 收藏
B. Sagheer, the Hausmeister time limit per test 1 second memory limit per test 256 megabytes input s ...
- Codefroces 812 B. Sagheer, the Hausmeister
http://codeforces.com/problemset/problem/812/B B. Sagheer, the Hausmeister time limit per test 1 sec ...
- 记忆化搜索(DP+DFS) URAL 1183 Brackets Sequence
题目传送门 /* 记忆化搜索(DP+DFS):dp[i][j] 表示第i到第j个字符,最少要加多少个括号 dp[x][x] = 1 一定要加一个括号:dp[x][y] = 0, x > y; 当 ...
- codeforces 812E Sagheer and Apple Tree(思维、nim博弈)
codeforces 812E Sagheer and Apple Tree 题意 一棵带点权有根树,保证所有叶子节点到根的距离同奇偶. 每次可以选择一个点,把它的点权删除x,它的某个儿子的点权增加x ...
随机推荐
- VMware Playerでの仮想マシン起動エラー
Windows Updateすると.翌日VMware Playerの仮想マシン起動時に 「この仮想マシンを構成済み設定でパワーオンするのに十分な物理メモリがありません.」 のエラーとなることが時々あり ...
- android应用开发-从设计到实现 3-4 静态原型的状态栏
静态原型的状态栏 状态栏Symbol 状态栏似乎非常复杂,有wifi信号.手机信号.时间.电量等信息,幸好Sketch原生就自带的现成组件,你能够直接拿过来就用了.当然.你也能够自己一个一个去画,只是 ...
- 06-Linux RPM 命令参数使用详解
rpm 执行安装包二进制包(Binary)以及源代码包(Source)两种.二进制包可以直接安装在计算机中,而源代码包将会由 RPM自动编译.安装.源代码包经常以src.rpm作为后缀名. 常用命令组 ...
- CMD 切换管理员权限
方法一:鼠标右键 这个方法比较比较普通,点开开始找到cmd,右击鼠标“以管理员身份运行(A)”这样调用就是管理员的权限: 方法二:快捷模式 在点开win+R后,选择“以管理员身份运行”,然后确定:可以 ...
- mysql存储引擎ARCHIVE
mysql常用引擎MyISAM和InnoDB,前者插入快 查询快,后者修改快 支持事务,各有优缺点,在网上突然看到一个引擎叫ARCHIVE,还蛮特别的 这个引擎只允许插入和查询,不允许修改和删除.相当 ...
- RF-字符串转为整数的方法
- Android开发-- 使用ADT23 的一些问题
在使用最新版ADT 23进行android学习时发现一些问题: 1.通过设置intent的action来启动另外一个activity时,会出现No Activity found to handle I ...
- Linux centos 下 eclipse 打开文件时关闭
原文地址:http://processors.wiki.ti.com/index.php/Linux_Host_Support#cairo-misc.c:380:_cairo_operator_bou ...
- Java单播、广播、多播(组播)
一.通信方式分类 在当前的网络通信中有三种通信模式:单播.广播和多播(组播),其中多播出现时间最晚,同时具备单播和广播的优点. 单播:单台主机与单台主机之间的通信 广播:当台主机与网络中的所有主机通信 ...
- django进阶-1
前言: 各位久等了,django进阶篇来了. 一.get与post 接口规范: url不能写动词,只能写名词 django默认只支持两种方式: get, post get是获取数据 ?user=zcl ...