Sagheer, the Hausmeister

CodeForces - 812B

题意:有一栋楼房,里面有很多盏灯没关,为了节约用电小L决定把这些灯都关了。

这楼有 n 层,最左边和最右边有楼梯。每一层有 m 个房间排成一排。这栋楼可以被表示成一个 nm + 2 列的矩阵,其中每行第一个和最后一个格点表示楼梯, 剩余 m 个格点表示房间。

现在小L在最底层的最左边楼梯,他想要关掉所有的灯。他每次可以走到相邻的房间,如果在楼梯口可以上下楼梯。他打算关掉所有开着的灯,在他没有将一层的所有灯都关闭前他不会上楼。现在求他最少需要走多少步可以关闭所有灯。

注意小L不需要返回原处,最终可以停留在任意一个地方。

直接dfs所有的状态:

#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
using namespace std;
int n,m;
char mapp[20][110];
int dfs(int cnt,int pos,int temp)//cnt表示楼层,pos表示是哪边的楼梯,temp表示的是上楼要走多少步
{
if(cnt<0) return 0;
int ans=0;
if(pos)
{
for(int i=m+2;i>=0;i--)
{
if(mapp[cnt][i]=='1')
{
ans+=pos-i+temp;
pos=i;
temp=0;
}
}
}
else
{
for(int i=0;i<m+2;i++)
{
if(mapp[cnt][i]=='1')
{
ans+=i-pos+temp;
pos=i;
temp=0;
}
}
}
ans+=min(dfs(cnt-1,0,temp+pos+1),dfs(cnt-1,m+1,temp+m+2-pos));
return ans;
} int main() {
cin.sync_with_stdio(false);
while (cin >> n >> m) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m + 2; j++) {
cin >> mapp[i][j];
}
}
cout << dfs(n-1,0,0) << endl;
}
return 0;
}

CodeForce-812B Sagheer, the Hausmeister(DFS)的更多相关文章

  1. #417 Div2 Problem B Sagheer, the Hausmeister (DFS && 枚举)

    题目链接:http://codeforces.com/contest/812/problem/B 题意 : 给出一个 n (1 ≤ n ≤ 15)层的教学楼, 每一层楼包含 m (1 ≤ m ≤ 10 ...

  2. CodeForces - 812B Sagheer, the Hausmeister 搜索 dp

    题意:给你n行长度为m的01串(n<15,m<100) .每次只能走一步,要将所有的1变为零,问最少的步数,注意从左下角开始,每次要将一层清完才能走到上一层,每次只有在第一列或者最后一列才 ...

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

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

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

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

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

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

  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. 【DFS】codeforces B. Sagheer, the Hausmeister

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

  9. 【codeforces 812B】Sagheer, the Hausmeister

    [题目链接]:http://codeforces.com/contest/812/problem/B [题意] 一个老大爷在一楼; 然后他有n楼的灯要关(最多n楼); 每楼有m个房间; 给出每个房间的 ...

随机推荐

  1. Note about Cobertura

    Workflow of Unit Test without Cobertura compile source code; compile test code; run unit test; Workf ...

  2. 机器学习:单元线性回归(python简单实现)

    文章简介 使用python简单实现机器学习中单元线性回归算法. 算法目的 该算法核心目的是为了求出假设函数h中多个theta的值,使得代入数据集合中的每个x,求得的h(x)与每个数据集合中的y的差值的 ...

  3. [编译] 9、在Linux下搭建 nordic 最新基于 zephyr 的开发烧写环境

    目录 前言 1.概述 2.安装工具 3.获取 nRF Connect SDK 源码 4.安装 Python modules 5.安装 toolchain 6.下载 nRF Command Line T ...

  4. SQL injection:Summary ,Overview and Classification

    What is SQL injection (SQLi)? SQL注入是一种web安全漏洞,让攻击者干扰应用程序对其数据库的查询. 它通常使得攻击者查看他们通常无法检索的数据. 这可能包括属于其他用户 ...

  5. ad 差分布线 等长布线

    差分要素: 1.原理图差分对名字后缀必须是 _n _p 2.规则改动 定义差分线宽和间距

  6. SQL 练习30

    查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) SELECT Student.*,CId,score from Student LEFT JOIN SC ON Student.SId = ...

  7. NOIP 模拟 9 数颜色

    题解 一道裸的数据结构题 正解是排序 \(+\) 二分,但是这怎么能有动态开点线段树好写呢? 于是我就打了暴力,骗了五十分. 对于每种颜色,我们在下标上开一颗线段树,对于交换若颜色相同则跳过,否则直接 ...

  8. RabbitMQ 安装与配置管理

    rabbitmq安装 1. 安装erlang yum install erlang xmlto 2. 安装rabbitmq rpm包 #wget http://www.rabbitmq.com/rel ...

  9. AECC2018同时中英文切换多开使用,加倍提高你的工作效率

    最近相信不少人已经更新了AECC2018,升级之后第一件重要的事当然是中英文的切换了,要不然工作中很麻烦.对于一直习惯用中文的人来说,在用模板过程中会出现各种表达式报错极其不方便,而对于习惯英文操作朋 ...

  10. 什么是.NET CLI CLR IL JIT GC,它们是如何工作的

    参考网址: https://cloud.tencent.com/developer/article/1432891 1:什么是.NET? NET 是 Microsoft 的用以创建 XML Web 服 ...