题意:给你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. Java实现经理与员工的差异

    对于在同一家公司工作的经历和员工而言,两者是有很多共同点的.例如,每个月都要发工资,但是经理在完成目标任务后,还会获得奖金.此时,利用员工类来编写经理类就会少写很多代码,利用继承技术可以让经理类使用员 ...

  2. SpringMVC由浅入深day01_13springmvc和struts2的区别_14问题

    13 springmvc和struts2的区别 1.springmvc是基于方法开发(一个url对应一个方法),请求参数传递到方法的形参,可以设计为单例或多例(建议单例),struts2是基于类开发, ...

  3. SpringMVC由浅入深day01_9商品修改功能开发

    9 商品修改功能开发 9.1 需求 操作流程: 1.进入商品查询列表页面 2.点击修改,进入商品修改页面,页面中显示了要修改的商品(从数据库查询) 要修改的商品从数据库查询,根据商品id(主键)查询商 ...

  4. Windows 2012 R2 安装Nessus

    1.nessus官网注册 注册地址:https://www.tenable.com/products/nessus-home Name字段随意,邮箱需要填写自己的,方便接受注册码 2.注册后,登录邮箱 ...

  5. 使用dshow抓取摄像头数据时,回调函数时间为0的问题

    在使用dshow抓取摄像头数据,调用dshow的回调函数,如果发现SampleTime一直为0,如下图 那极有可能是使用RenderStream函数连接Filter时,指定的第一个参数为 PIN_CA ...

  6. 使用pyenv管理不同的python版本

    1. pvenv的安装 git clone https://github.com/yyuu/pyenv.git ~/.pyenv echo 'export PYENV_ROOT="$HOME ...

  7. Android 监听apk安装替换卸载广播

    首先是要获取应用的安装状态,通过广播的形式 以下是和应用程序相关的Broadcast Action ACTION_PACKAGE_ADDED 一个新应用包已经安装在设备上,数据包括包名(最新安装的包程 ...

  8. flask操作mongo两种方式--常规

    #manage.py #coding=utf-8 #Flask-Script是一个可以在flask应用外部编写脚本的扩展 #常用功能: #运行一个开发的服务器 #python shell中操作数据库看 ...

  9. 解决node里面的中文乱码

    今天咋学习node的时候,跟着视频里在撸代码,但是却出现了中文乱码的情况,视频中的谷歌浏览器可能和我的版本不一致,先看代码吧: 'use strict'; const http = require(& ...

  10. PHP一致性哈希实现。。

    <?php /** *@author:xiaojiang 20140222 * 一致性哈希php 实现 */ class MyHash{ //虚拟节点数 private $_virtualCou ...