Sagheer, the Hausmeister

CodeForces - 812B

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

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

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

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

直接dfs所有的状态:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<string>
  4. #include<algorithm>
  5. using namespace std;
  6. int n,m;
  7. char mapp[20][110];
  8. int dfs(int cnt,int pos,int temp)//cnt表示楼层,pos表示是哪边的楼梯,temp表示的是上楼要走多少步
  9. {
  10. if(cnt<0) return 0;
  11. int ans=0;
  12. if(pos)
  13. {
  14. for(int i=m+2;i>=0;i--)
  15. {
  16. if(mapp[cnt][i]=='1')
  17. {
  18. ans+=pos-i+temp;
  19. pos=i;
  20. temp=0;
  21. }
  22. }
  23. }
  24. else
  25. {
  26. for(int i=0;i<m+2;i++)
  27. {
  28. if(mapp[cnt][i]=='1')
  29. {
  30. ans+=i-pos+temp;
  31. pos=i;
  32. temp=0;
  33. }
  34. }
  35. }
  36. ans+=min(dfs(cnt-1,0,temp+pos+1),dfs(cnt-1,m+1,temp+m+2-pos));
  37. return ans;
  38. }
  39.  
  40. int main() {
  41. cin.sync_with_stdio(false);
  42. while (cin >> n >> m) {
  43. for (int i = 0; i < n; i++) {
  44. for (int j = 0; j < m + 2; j++) {
  45. cin >> mapp[i][j];
  46. }
  47. }
  48. cout << dfs(n-1,0,0) << endl;
  49. }
  50. return 0;
  51. }

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. U盘自动弹出脚本

    需要微软的Sysinternals Suite中的sync工具,解压到d:\apps下. ahk脚本: #u:: ; eject usb drive InputBox, myInp, Remove U ...

  2. Linux 硬盘与硬件管理

    硬件以文件系统(Filesystem)角度来看 文件系统:一个可被挂载的数据称为文件系统,每个操作系统可以使用的文件系统并不一样,windows98是FAT或者FAT16文件系统,而windows20 ...

  3. Sqli-Labs less1-4

    首先,记录一下基础知识,可能不全: 几个常用的函数: 1.version()   --Mysql版本 2.user()  --数据库用户名 3.database()  --数据库名 4.@@datad ...

  4. stm32 connot enter debug mode

    dap 可以发现设备,stlink jlink 均无法发现设备,但是都不能下载.connot enter debug mode ,发现是vdda 未连接

  5. 原来一条select语句在MySQL是这样执行的《死磕MySQL系列 一》

    前言 看到蒋老师的第一篇文章后就收货颇丰,真是句句戳中痛点. 令我记忆最深的就是为什么知道了一个个技术点,却还是用不好 ?不管是蒋老师所说的Redis还是本系列要展开学习的MySQL. 这是一个值得思 ...

  6. Java异常处理的两种方式以及自定义异常的使用方法

    异常 就是程序出现了不正常的情况 Error:严重问题,不需要处理 Exception:称为异常类,他表示程序本身可以处理的问题 RuntimeException:在编译期是不检查的,出现问题后,需要 ...

  7. SQL 练习35

    查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 方式1: SELECT Student.sid,Student.sname,t.score from Student , (SELEC ...

  8. docker 安装 sonarQube

    sonarQube 是一款开源代码检测工具.本篇介绍通过 docker 来安装.大概的一个运作流程是这样的,先通过 sonar-scanner 插件扫描代码,把数据存储到数据库,sonarQube 读 ...

  9. 依赖注入@Autowired@Primary@Quelifier使用

    @Autowired 注入声明的SpringBean对象,根据一定的规则首先按照注入的类型去查找,如果没有找到安装注入的名称去匹配你要注入的属性名称,如果都没有找到启动项目时抛出异常,@Autowir ...

  10. npm压缩js文件

    参考:https://blog.csdn.net/msy_msy/article/details/78261383 1.压缩单个js文件 cnpm install uglify-js -g 安装 1& ...