题意:给你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的更多相关文章

  1. 【DFS】codeforces B. Sagheer, the Hausmeister

    http://codeforces.com/contest/812/problem/B [题意] 有一个n*m的棋盘,每个小格子有0或1两种状态,现在要把所有的1都变成0,问最少的步数是多少?初始位置 ...

  2. AC日记——Sagheer, the Hausmeister codeforces 812b

    812B - Sagheer, the Hausmeister 思路: 搜索: 代码: #include <cstdio> #include <cstring> #includ ...

  3. 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 ...

  4. Codeforces Round #417 B. Sagheer, the Hausmeister

    B. Sagheer, the Hausmeister time limit per test  1 second memory limit per test  256 megabytes   Som ...

  5. CodeForce-812B Sagheer, the Hausmeister(DFS)

    Sagheer, the Hausmeister CodeForces - 812B 题意:有一栋楼房,里面有很多盏灯没关,为了节约用电小L决定把这些灯都关了. 这楼有 n 层,最左边和最右边有楼梯. ...

  6. 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 ...

  7. Codefroces 812 B. Sagheer, the Hausmeister

    http://codeforces.com/problemset/problem/812/B B. Sagheer, the Hausmeister time limit per test 1 sec ...

  8. 记忆化搜索(DP+DFS) URAL 1183 Brackets Sequence

    题目传送门 /* 记忆化搜索(DP+DFS):dp[i][j] 表示第i到第j个字符,最少要加多少个括号 dp[x][x] = 1 一定要加一个括号:dp[x][y] = 0, x > y; 当 ...

  9. codeforces 812E Sagheer and Apple Tree(思维、nim博弈)

    codeforces 812E Sagheer and Apple Tree 题意 一棵带点权有根树,保证所有叶子节点到根的距离同奇偶. 每次可以选择一个点,把它的点权删除x,它的某个儿子的点权增加x ...

随机推荐

  1. Apache双机热备

    部署方案 1.1 方案设计 1.2 方案描述 如上图所示,我们要有三个可用的IP地址(切记不能与网络中其他机器IP重复),针对我使用的三个IP地址做如下说明: 10.16.252.10 //这个IP地 ...

  2. Python "HTTP Error 403: Forbidden"

    问题: 执行下面的语句时 def set_IPlsit(): url = 'https://www.whatismyip.com/' response = urllib.request.urlopen ...

  3. Ansible 使用 Playbook 安装 Nginx

    思路:先在一台机器上编译安装好 Nginx,打包,然后通过 Ansible 下发 [root@localhost ~]$ cd /etc/ansible/ [root@localhost ansibl ...

  4. Steam安装Google Earth VR

    打开Steam 打开火狐浏览器 输入steam://install/348250

  5. python中json格式数据输出实现方式

    python中json格式数据输出实现方式 主要使用json模块,直接导入import json即可. 小例子如下: #coding=UTF-8 import json info={} info[&q ...

  6. apache下配置php环境

    1. apache下载 http://httpd.apache.org/download.cgi 2. php下载 http://windows.php.net/download/ 3. 配置 apa ...

  7. Python学习(21):Python函数(5):变量作用域与闭包

    转自 http://www.cnblogs.com/BeginMan/p/3179040.html 一.全局变量与局部变量 一个模块中,最高级别的变量有全局作用域. 全局变量一个特征就是:除非被删除, ...

  8. Linux调试分析诊断利器——strace

    strace是个功能强大的Linux调试分析诊断工具,可用于跟踪程序执行时进程系统调用(system call)和所接收的信号,尤其是针对源码不可读或源码无法再编译的程序. 在Linux系统中,用户程 ...

  9. 【大数据系列】HDFS初识

    一.HDFS介绍 HDFS为了做到可靠性(reliability)创建了多分数据块(data blocks)的复制(replicas),并将它们放置在服务集群的计算节点中(compute nodes) ...

  10. Android.mk(4) 依赖:目标编程的模式

    https://www.jianshu.com/p/3777a585a8d0 另一种范式 我一直觉得,Makefile确实是C/C++程序员的良配,因为Makefile所使用的两种范式都是C/C++程 ...