您的位置:首页 > 其它

sgu239 分类: sgu 2015-06-24 22:18 22人阅读 评论(0) 收藏

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

记 ai为位置 i上雷的数量,bi为位置 i上的数字

bi=ai−1+ai+ai+1⇒ai=bi−1−ai−1−ai−2

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