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 ...
随机推荐
- Ubuntu 16.04服务器 配置
1. 修改用户名称:切换到root打开如下两个配置文件 sudo vim /etc/passwd 把我想改的"xxx"这个用户名改为"way"了,保存并退出 s ...
- HTML开发之(块级标签,行内标签,行内块标签)
显示模式的特性: 主要分为两大类: 块级元素:独占一行,对宽高的属性值生效:如果不给宽度,块级元素就默认为浏览器的宽度,即就是100%宽: 行内元素:可以多个标签存在一行,对宽高属性值不生效,完全靠内 ...
- iOS 关于信鸽推送点击推送通知的处理
最近的项目中使用了推送模块,使用的是企鹅帝国的信鸽推送服务,关于具体怎么推送的,证书如何设置,我不再赘述,一来开发文档中已经讲的非常清楚,二来在网上一搜的话也能搜到一大堆:在这里主要写下关于推送的通知 ...
- 转载 IMP时数据库的IO性能监控,并提供IOPS的计算方法
IMP时数据库的IO性能监控,并提供IOPS的计算方法 2011-07-15 17:36:10 分类: Linux [root@ntkdb oradata]# iostat -x 1 10 ...
- Ansible Playbook 简介
我们去远程执行命令时要使用 command 模块,拷贝文件时要使用 copy 模块,如果我们要操作的东西很多,那就要执行很多条不同模块的命令Playbook 是一个 yaml 配置文件,我们可以把不同 ...
- Python标准异常和异常处理详解
python提供了两个非常重要的功能来处理python程序在运行中出现的异常和错误.你可以使用该功能来调试python程序. 1.异常处理: 本站Python教程会具体介绍. 2.断言(Asserti ...
- Ubuntu下安装MySQL及简单操作
Ubuntu上安装MySQL非常简单只需要几条命令就可以完成. 1. sudo apt-get install mysql-server 2. apt-get isntall mysql-client ...
- 关于Android开发中使用的XML
1.布局 FrameLayout: 以堆叠方式显示一个或多个子视图. GridLayout: 将子视图按行和列排列. LinearLayout: 将所有子视图排列成一行或一列. RelativeLay ...
- linux下删除大量小文件
当目录下文件太多时,用rm删除文件会报错:-bash: /bin/rm: Argument list too long提示文件数目太多.解决的办法是使用如下命令:ls | xargs -n 10 rm ...
- R语言(入门小练习篇)
问题: 一组学生参加了数学.科学和英语考试.为了给所有的学生确定一个单一的成绩衡量指标,需要将这些科目的成绩组合起来.另外,还想将前20%的学生评定为A,接下来20%的学生评定为B,以此类推.最后,希 ...