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. Use w3m as Web Browser

    Installation: apt-get install w3m. use "a" to input text, "tab" to jump between ...

  2. WPF 线程开启等待动画

    public static Dictionary<string, object> Dic = new Dictionary<string, object>();//定义一个字典 ...

  3. Golang语言系列-13-常用内置包

    常用内置包 net/http包 http请求和响应 http服务端 main.go文件 package main import ( "fmt" "io/ioutil&qu ...

  4. 【笔记】numpy.array基础(2)

    numpy数组的基本操作 以几个数组为例 使用ndim可以显示出是几维数组 使用shape可以查看元素维度数 使用size可以查看元素个数 对一维数组进行数据访问 对多维数组进行数据访问 切片访问,使 ...

  5. linux联网、远程

    使用VMware虚拟机创建linux系统CentOS 7,连接网络以及远程Moba工具. 一.首先安装好VMware虚拟机和CentOS系统,网上很多教程,不在赘述. 二.打开VMware进行配置 1 ...

  6. NOIP 模拟 $13\; \text{工业题}$

    题解 本题不用什么推式子,找规律(而且也找不出来) 可以将整个式子看成一个 \(n×m\) 矩阵 考虑 \(f_{i,j}\),它向右走一步给出 \(f_{i,j}×a\) 的贡献,向下走一步给出 \ ...

  7. visual studio code 中文

    1.按住ctrl+shift+p键,在框中输入configure,在下拉选项中选取language选项 2.打开locale.json文件,修改语言配置 3.修改完保存,然后重新启动vscode 4. ...

  8. MyBatis的useGeneratedKeys使用

    业务需求,用户表为主键自增,添加完用户之后,通过用户ID和角色表进行关联. 问题:由于主键自增,所以在用户添加之前是不知道ID的,当然可以通过查询得到当前的ID,不过需要自己多一步操作. 解决方案:使 ...

  9. C++面试题(四)——智能指针的原理和实现

    C++面试题(一).(二)和(三)都搞定的话,恭喜你来到这里,这基本就是c++面试题的最后一波了.     1,你知道智能指针吗?智能指针的原理.     2,常用的智能指针.     3,智能指针的 ...

  10. 15.SpringMVC之异步请求

    SpringMVC中异步请求相关组件 SpringMVC在此基础上对异步请求进行了封装.提供了AsyncWebRequest类型的request,并提供了处理异步请求的管理器WebAsyncManag ...