您的位置:首页 > 其它

sgu239

2015-06-24 22:18 411 查看
如果知道第一个方格是否有雷,可以推出其他所有方格是否有雷:

记 aia_i为位置 ii上雷的数量,bib_i为位置 ii上的数字

bi=ai−1+ai+ai+1⇒ai=bi−1−ai−1−ai−2b_i = a_{i-1}+a_i+a_{i+1} \Rightarrow a_i = b_{i-1} - a_{i-1}-a_{i-2}

只要枚举 a1a_1的值,判断是否可行即可。

时间复杂度:O(n)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;       
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: