sgu239


如果知道第一个方格是否有雷,可以推出其他所有方格是否有雷:

ai 为位置 i 上雷的数量,bi为位置 i 上的数字
bi=ai1+ai+ai+1ai=bi1ai1ai2


只要枚举 a1 的值,判断是否可行即可。
时间复杂度: O(n)


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>


const int maxn = 1005;
int n, b[maxn];
int ans, a[maxn];

int check(int x)
{
    a[1] = x;

    for(int i = 2; i <= n+1; i++)
    {
        a[i] = b[i-1] - a[i-1] - a[i-2];

        if(!(a[i] == 0 || a[i] == 1))
            return 0;
    }
    if(a[n+1]) return 0;

    return 1;
}

int main()
{
#ifndef ONLINE_JUDGE    
    freopen("sgu239.in","r",stdin);
    freopen("sgu239.out","w",stdout);
#endif

    std::cin >> n;

    for(int i = 1; i <= n; i++)
        std::cin >> b[i];

    ans = check(0) + check(1);

    std::cout << ans;

#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;       
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值