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个房间; 给出每个房间的 ...
随机推荐
- ESP32-S2原生USB 烧录 TinyUF2 bootloader 加 CircuitPython
概述 ESP32-S2最令我心仪的改进是原生支持USB,即带有一个集成了收发器的全速 USB OTG 外设,符合 USB 1.1 规范,理论速度1.5m/s,利用得当将会是一个非常巨大的进步. 目前E ...
- Linux搭建VNC servere服务
此安装方法只适用于centos7以上的版本 一,安装 以root用户运行以下命令来安装vncserver; yum install tigervnc-server 同样运行以下命令来安装vncvie ...
- docker安装maven私服
目录 一.nexus3安装 二.创建私服仓库 三.发布jar包到私服 四.引用maven私服jar包 五.参考 一.nexus3安装 1.安装镜像 docker pull sonatype/nexus ...
- C++11 shared_ptr智能指针(超级详细)
在实际的 C++ 开发中,我们经常会遇到诸如程序运行中突然崩溃.程序运行所用内存越来越多最终不得不重启等问题,这些问题往往都是内存资源管理不当造成的.比如: 有些内存资源已经被释放,但指向它的指针并没 ...
- 十二:Servlet3.0的注解
1.@WebListener注解 表示的就是我们之前的在xml中配置的 <listener> <listener-class>ListenerClass</listene ...
- LeetCoded第25题题解--K个一组翻转链表--java--链表
链表 单链表:链表中的每个元素实际上是一个单独的对象,而所有对象都通过每个元素的引用字段链接在一起. 双链表:与单链表不同的是,双链表的每个节点都含有两个引用字段. 链表优点 灵活分配内存空间 能在O ...
- 进程上下文&中断上下文
文章出自http://hi.baidu.com/bkhcvzdvmjfkpyr/item/5444001fa68d065bf1090ea6 处理器总处于以下状态中的一种: 1.内核态,运行于进程上下文 ...
- flink双流join
package com.streamingjoin import org.apache.flink.api.common.state.{ValueState, ValueStateDescriptor ...
- ks.cfg文件相关
原文转自:https://www.cnblogs.com/itzgr/p/10029631.html作者:木二 目录 一 图形化生成ks.cfg文件 二 ks.cfg文件相关项解析 一 图形化生成ks ...
- 分布式ID生成器及redis,etcd分布式锁
分布式id生成器 有时我们需要能够生成类似MySQL自增ID这样不断增大,同时又不会重复的id.以支持业务中的高并发场景.比较典型的,电商促销时,短时间内会有大量的订单涌入到系统,比如每秒10w+.明 ...