AC日记——Sagheer, the Hausmeister codeforces 812b

812B - Sagheer, the Hausmeister

思路:

  搜索;

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

#define maxn 20
#define maxm 105
#define INF 0x7fffffff

int n,m,deep[maxn][2],num[maxn],ans=INF,k,sum[maxn],cnt;

char map[maxn][maxm];

void dfs(int now,bool st,int val)
{
    if(val>=ans) return;
    if(now==n)
    {
        if(num[now])
        {
            if(st) val+=k-deep[now][0];
            else val+=deep[now][1]-1;
        }
        if(val<ans) ans=val;
        return;
    }
    if(sum[now]==cnt)
    {
        if(num[now])
        {
            if(st) val+=k-deep[now][0];
            else val+=deep[now][1]-1;
        }
        if(val<ans) ans=val;
        return;
    }
    if(num[now])
    {
        if(st) dfs(now+1,st^1,val+k),dfs(now+1,st,val+(k-deep[now][0])*2+1);
        else dfs(now+1,st,val+deep[now][1]*2-1),dfs(now+1,st^1,val+k);
    }
    else dfs(now+1,st,val+1),dfs(now+1,st^1,val+k);
}

int main()
{
    scanf("%d%d",&n,&m);k=m+2;
    for(int i=1,i_=n;i<=n;i++,i_=n-i+1)
    {
        scanf("%s",map[i]+1);
        for(int j=1;j<=m+2;j++)
        {
            if(map[i][j]==‘1‘&&(!deep[i_][0])) deep[i_][0]=j;
            if(map[i][j]==‘1‘) deep[i_][1]=j,num[i_]++;
        }
    }
    for(int i=1;i<=n;i++) sum[i]=sum[i-1]+num[i],cnt+=num[i];
    dfs(1,false,0),printf("%d\n",ans);
    return 0;
}
时间: 2024-12-16 22:05:00

AC日记——Sagheer, the Hausmeister codeforces 812b的相关文章

AC日记——Sagheer and Crossroads codeforces 812a

812A - Sagheer and Crossroads 思路: 模拟: 代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define Yes {puts("YES\n");return 0;} int ai[5][5]; int main() { for(int i=1;i&l

AC日记——Dynamic Problem Scoring codeforces 807d

Dynamic Problem Scoring 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn 130 int n,ai[maxn][6],ac[6],cnt,all,last1,last2; double map[3][6]; inline void in(i

AC日记——Is it rated? codeforces 807a

Is it rated? 思路: 水题: 代码: #include <cstdio> #include <cstring> using namespace std; int n,a[1001],b[1001],last=5000; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d%d",&a[i],&b[i]); if(a[i]!=b[i

AC日记——Periodic RMQ Problem codeforces 803G

G - Periodic RMQ Problem 思路: 题目给一段序列,然后序列复制很多次: 维护序列很多次后的性质: 线段树动态开点: 来,上代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn 100005 struct TreeNodeType { int l, r, mid

CodeForce-812B Sagheer, the Hausmeister(DFS)

Sagheer, the Hausmeister CodeForces - 812B 题意:有一栋楼房,里面有很多盏灯没关,为了节约用电小L决定把这些灯都关了. 这楼有 n 层,最左边和最右边有楼梯.每一层有 m 个房间排成一排.这栋楼可以被表示成一个 n 行 m?+?2 列的矩阵,其中每行第一个和最后一个格点表示楼梯, 剩余 m 个格点表示房间. 现在小L在最底层的最左边楼梯,他想要关掉所有的灯.他每次可以走到相邻的房间,如果在楼梯口可以上下楼梯.他打算关掉所有开着的灯,在他没有将一层的所有灯

Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister(DP)

题目链接:Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister 题意: 有n层楼,每层有m个房间,每层的两边是楼梯. 现在有一个人站在左下角,这个人必须将这一层的灯关闭后才能去另外一层. 每移动一次需要1分钟,问关闭所有灯需要多少时间. 题解: 考虑DP[i][j]表示当前已经关闭了第i层全部的灯,j=0时表示在这一层的最左边,j=1时表示在这一层的最右边. 然后推上去就行了.最后讨论一下特殊情况. 1 #include<bits/

AC日记——Aragorn&#39;s Story HDU 3966

Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10510    Accepted Submission(s): 2766 Problem Description Our protagonist is the handsome human prince Aragorn comes from The Lor

CodeForces - 812B Sagheer, the Hausmeister

题意:给你 #define _CRT_SECURE_NO_WARNINGS #include<cstdio> #include<algorithm> #include<iostream> #include<string> using namespace std; typedef long long ll; const int maxn = 2e5 + 5; int m, n,len; int dp[20][2];//清完i层回到最左0右1 所需的min in

AC日记——Success Rate codeforces 807c

Success Rate 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define ll long long inline void in(ll &now) { char Cget=getchar();now=0; while(Cget>'9'||Cget<'0