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个房间; 给出每个房间的 ...
随机推荐
- brew换源
转自:https://blog.csdn.net/gorwayne/article/details/107359912 第一步,替换brew.git cd "$(brew --repo)&q ...
- 一个命令搞定 Web 国际化
背景 随着出海的业务越来越多,web 应用面临越来越多的国际化的工作.如何高效,高质量的完成 Web 前端国际化工作,已经是摆在 web 前端同学的急需解决的问题. i18n-helper-cli 是 ...
- linux下安装redis-6.0.6、配置redis远程连接
官网下载安装包redis-6.0.6.tar.gz https://redis.io/ 上传到服务器之后使用tar -zxvf进行解压,解压后如下: 进入解压的文件之后我们可以看到他的配置文件(配置文 ...
- WPF 饼状图,柱形图,折线图 (1 柱形图)
WPF三贱客绘制,柱形图应该是比较简单的一个了.效果如下: ItemSource数据结构可自己定义,我的如下列子,自定义的数据结构属性,要对应配置下DisplayMemberMsg 和DisplayM ...
- Qt 中的属性系统(Property System)
21 人赞同了该文章 本节内容主要讲解我对 Qt 属性系统的理解.官方文档参考 The Property System. 如何理解"属性系统"这个概念? 一般我们说一个类有什么属性 ...
- CentOS8安装Mysql5.7
检查是否安装mysql [root@iZ2ze8crquorxf6c7l0eluZ ~]# rpm -qa |grep mysql [root@iZ2ze8crquorxf6c7l0eluZ ~]# ...
- Consul 入门-初识
背景 现状:单体架构逐渐被微服务架构所替代,原本两个功能模被拆分成了两个服务.原本两个模块块间的通信只需要函数调用就能够实现,现在却做不到了,因为它们不在同一个进程中,甚至两个服务都可能部署到不同的机 ...
- IT项目经理-成长手记学习笔记
无论多难,都要记住一点,只要别人不赶你走,你就厚着脸皮待下去,这样你才有可能熬到项目成功. 项目经理要管事,更要管人. 项目计划->职责分工->确定项目范围 遇事及时处理,当场处理,处理错 ...
- default错误
查看根目录发现 修改成index.php可以了
- Java反射的浅显理解
一.回顾反射相关的知识 1.在xml文件中使用反射的好处: 1)代码更加灵活,后期维护只需要修改配置文件即可 · 初学者一般习惯于在代码本身上直接修改,后期也可以修改配置文件达到相同的目的 · 修改配 ...