CodeForce-812B Sagheer, the Hausmeister(DFS)
Sagheer, the Hausmeister
题意:有一栋楼房,里面有很多盏灯没关,为了节约用电小L决定把这些灯都关了。
这楼有 n 层,最左边和最右边有楼梯。每一层有 m 个房间排成一排。这栋楼可以被表示成一个 n 行 m + 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)的更多相关文章
- #417 Div2 Problem B Sagheer, the Hausmeister (DFS && 枚举)
题目链接:http://codeforces.com/contest/812/problem/B 题意 : 给出一个 n (1 ≤ n ≤ 15)层的教学楼, 每一层楼包含 m (1 ≤ m ≤ 10 ...
- CodeForces - 812B Sagheer, the Hausmeister 搜索 dp
题意:给你n行长度为m的01串(n<15,m<100) .每次只能走一步,要将所有的1变为零,问最少的步数,注意从左下角开始,每次要将一层清完才能走到上一层,每次只有在第一列或者最后一列才 ...
- AC日记——Sagheer, the Hausmeister codeforces 812b
812B - Sagheer, the Hausmeister 思路: 搜索: 代码: #include <cstdio> #include <cstring> #includ ...
- 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 ...
- Codeforces Round #417 B. Sagheer, the Hausmeister
B. Sagheer, the Hausmeister time limit per test 1 second memory limit per test 256 megabytes Som ...
- 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 ...
- Codefroces 812 B. Sagheer, the Hausmeister
http://codeforces.com/problemset/problem/812/B B. Sagheer, the Hausmeister time limit per test 1 sec ...
- 【DFS】codeforces B. Sagheer, the Hausmeister
http://codeforces.com/contest/812/problem/B [题意] 有一个n*m的棋盘,每个小格子有0或1两种状态,现在要把所有的1都变成0,问最少的步数是多少?初始位置 ...
- 【codeforces 812B】Sagheer, the Hausmeister
[题目链接]:http://codeforces.com/contest/812/problem/B [题意] 一个老大爷在一楼; 然后他有n楼的灯要关(最多n楼); 每楼有m个房间; 给出每个房间的 ...
随机推荐
- 线程休眠_sleep
线程休眠_sleep sleep(时间)指定当前线程阻塞的毫秒数: sleep存在异常InterruptedException: sleep时间到达后线程进入就绪状态: sleep可以模拟网络延时,倒 ...
- Linux进程理解与实践(五)细谈守护进程
一. 守护进程及其特性 守护进程最重要的特性是后台运行.在这一点上DOS下的常驻内存程序TSR与之相似.其次,守护进程必须与其运行前的环境隔离开来.这些环境包括未关闭的文件描述符,控制终端, ...
- 【Mongodb】数据库备份与还原
Mongodb 备份与还原 Mongodb 备份与还原 文件快照 快照备份 快照直接还原 从压缩文件还原 复制文件 备份文件 从文件还原 mongodump mongodump备份 mongodump ...
- STM32—驱动HC-SR04超声波测距模块
文章目录 超声波测距原理 HC-SR04工作原理 STM32实现驱动 1.引脚的配置 2.时序控制 3.时间差测量 4.如何将距离测出来 超声波测距原理 利用HC-SR04超声波测距模块可以实现比较精 ...
- Android WorkManager工作约束,延迟与查询工作
WorkManager工作约束,延迟与查询工作 本文可能会混用"工作"与"任务"这两个词. 本文例子使用Kotlin 准备一个工作类(任务)UploadWork ...
- 在java程序中使用protobuf
目录 简介 为什么使用protobuf 定义.proto文件 编译协议文件 详解生成的文件 Builders 和 Messages 序列化和反序列化 协议扩展 总结 简介 Protocol Buffe ...
- Linux+Apache+Mysql+PHP简单的测试环境搭建
系统版本为:Linux localhost.localdomain 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x ...
- ffmpeg第2篇:简单滤镜与复杂滤镜的区别
在ffmpeg的滤镜中,有简单滤镜(simple filter)和复杂滤镜(complex filter)两种. 使用简单滤镜时,用-vf选项,使用复杂滤镜时,使用-filter_complex或-l ...
- Linux 记录学习
- 4、二进制安装K8s 之 部署kube-controller-manager
二进制安装K8s 之 部署kube-controller-manager 1.创建配置文件 cat > /data/k8s/config/kube-controller-manager.conf ...