【DFS】codeforces B. Sagheer, the Hausmeister
http://codeforces.com/contest/812/problem/B
【题意】
有一个n*m的棋盘,每个小格子有0或1两种状态,现在要把所有的1都变成0,问最少的步数是多少?初始位置在左下角,只有把下面一层的1都变成0后才可以到上一层,只有在每层的最右边和最左边可以向上走(up),否则只能左右移动(left or right)。只要经过1,就可以把1变成0,只要把最后一个1,就可以立即停止操作。
【Accepted】
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <bitset>
#include <ctime>
#include<algorithm>
#include<cstring>
using namespace std;
int n,m;
const int maxn=1e2+;
char s[maxn];
int a[][maxn];
int sum[];
int ans;
int fir[];
int sec[];
const int inf=0x3f3f3f3f;
int index;
void dfs(int cur,int flag,int cnt)
{
if(cur==index)
{
if(flag)
{
int t=cnt+sec[index]-;
ans=min(ans,t);
return;
}
else
{
int t=cnt+m+-fir[index];
ans=min(ans,t);
return;
}
}
if(flag)
{
if(sum[cur])
{
dfs(cur-,,cnt+m+);
dfs(cur-,,cnt+*(sec[cur]-)+);
}
else
{
dfs(cur-,,cnt+);
}
}
else
{
if(sum[cur])
{
dfs(cur-,,cnt+m+);
dfs(cur-,,cnt+*(m+-fir[cur])+);
}
else
{
dfs(cur-,,cnt+);
}
} }
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(sum,,sizeof(sum));
memset(fir,-,sizeof(fir));
memset(sec,,sizeof(sec));
ans=inf;
for(int i=;i<=n;i++)
{
scanf("%s",s+);
for(int k=;k<=m+;k++)
{
a[i][k]=s[k]-'';
sum[i]+=a[i][k];
//每层最右的1的位置
if(a[i][k])
{
sec[i]=k;
}
//每层最左的1的位置
if(a[i][k]&&fir[i]==-)
{
fir[i]=k;
}
}
}
int cou=;
//index表示走到第几层为止,因为上面的都是0
index=;
for(int i=;i<=n;i++)
{
cou+=sum[i];
if(cou!=)
{
index=i;
break;
}
}
//特判,如果数据都是0
if(index==)
{
printf("0\n");
continue;
}
//分别表示第几层,当前层的初始位置在最左边还是最右边,步数
dfs(n,,);
cout<<ans<<endl;
}
return ;
}
DFS:2^15
时间复杂度是2^15
没有考虑的都是0的corner case,疯狂wa
【DFS】codeforces B. Sagheer, the Hausmeister的更多相关文章
- 【dfs】codeforces Journey
http://codeforces.com/contest/839/problem/C [AC] #include<iostream> #include<cstdio> #in ...
- 【推导】【DFS】Codeforces Round #429 (Div. 1) B. Leha and another game about graph
题意:给你一张图,给你每个点的权值,要么是-1,要么是1,要么是0.如果是-1就不用管,否则就要删除图中的某些边,使得该点的度数 mod 2等于该点的权值.让你输出一个留边的方案. 首先如果图内有-1 ...
- 【贪心】【DFS】Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) C. Andryusha and Colored Balloons
从任意点出发,贪心染色即可. #include<cstdio> #include<algorithm> using namespace std; int v[200010< ...
- 【DFS】Codeforces Round #402 (Div. 2) B. Weird Rounding
暴搜 #include<cstdio> #include<algorithm> using namespace std; int n,K,Div=1,a[21],m,ans=1 ...
- 【DFS】Codeforces Round #398 (Div. 2) C. Garland
设sum是所有灯泡的亮度之和 有两种情况: 一种是存在结点U和V,U是V的祖先,并且U的子树权值和为sum/3*2,且U不是根,且V的子树权值和为sum/3. 另一种是存在结点U和V,他们之间没有祖先 ...
- 【第40套模拟题】【noip2011_mayan】解题报告【map】【数论】【dfs】
目录:1.潜伏者 [map] 2.Hankson的趣味题[数论]3.mayan游戏[dfs] 题目: 1. 潜伏者(spy.pas/c/cpp)[问题描述]R 国和S 国正陷入战火之中,双方都互派间谍 ...
- Kattis - glitchbot 【DFS】
Kattis - glitchbot [DFS] 题意 有一个机器人 刚开始在(0, 0),然后给出一个目标点,并且会给出一系列指令,但是其中会有一个指令是错误的.我们需要找出那个指令,并且改成正确的 ...
- 【题解】Codeforces 961G Partitions
[题解]Codeforces 961G Partitions cf961G 好题啊哭了,但是如果没有不小心看了一下pdf后面一页的提示根本想不到 题意 已知\(U=\{w_i\}\),求: \[ \s ...
- HDU 6113 度度熊的01世界 【DFS】(2017"百度之星"程序设计大赛 - 初赛(A))
度度熊的01世界 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
随机推荐
- EmitMapper系列之二:EmitMapper的使用小结
EmitMapper的入门 EmitMapper引用 EmitMapper案例 最近公司开发项目前端使用一个js框架,后端使用ef,js前台读取的json采用实体的dto来进行生成. 在网上看到了Em ...
- iOS面试题之runloop
本文围绕以下几个部分展开对runloop的叙述. 1.runloop是什么/runloop的概念? 2.NSRunLoop 和 CFRunLoopRef? 3.runloop和线程的关系? 4.run ...
- NIO服务端主要创建过程
NIO服务端主要创建过程: 步骤一:打开ServerSocketChannel,用于监听客户端的连接,它是所有客户端连接的副管道,示例代码如下: ServerSocketChannel ...
- Axure-计算输入字数
说明:Axure版本为7.0 1.添加多行文本框,设置名称为Input,添加文本框,设置名称为msg,样式如下: 2.为input添加“文本改变时”事件,设置全局变量,如下所示: 3.再添加“设置文本 ...
- 【opencv】imread CV_LOAD_IMAGE_GRAYSCALE
转灰度图的操作很多,但是opencv中的CV_LOAD_IMAGE_GRAYSCALE的具体操作为: gray = 0.299 * r + 0.587 * g + 0.114 * b 然后,小数点部分 ...
- Unity3D 在自定义脚本中实现Button组件上的OnClick面板
下述内容不对c#语法做过多讲解,仅对已入门并有兴趣的同学做为学习和拓展的资料 大家在Unity制作的过程中一定都使用过UI功能,那么很多人也一定见过这个面板: 那么我们如何能在自己的脚本中添加上像On ...
- mysql 函数tree状
// 子节点的查询 CREATE DEFINER = `root`@`%` FUNCTION `getDeptChildList`(rootId BIGINT) RETURNS longtext DE ...
- VBA Promming入门教程——变量的使用
数据类型 VBA中的数据类型可分为两种 示例 String Sub Main Dim s as string s = "Hello" msgbox(s) End Sub Singl ...
- Mathematics-基础:1+2+3+……+n
设Sn=1+2+3+……+n-1+n则有Sn=n+n-1+……+3+2+1两式相加得2Sn=(n+1)+(n+1)+……+(n+1)2Sn=n×(n+1)Sn=n×(n+1)/2
- PHP23 AJAX分页
模型代码设计 以留言信息管理为例. 获取根据条件查询记录总数和分页数据. <?php namespace application\admin\models; use core\mybase\Mo ...