BZOJ1088或洛谷2327 [SCOI2005]扫雷

BZOJ原题链接

洛谷原题链接

很容易发现答案就只有(0,1,2)三种答案,而且只要知道第一个格子是否有雷就可以直接顺推下去了。
所以我们跑一次首位有雷,跑一次首位无雷判断是否可行即可。

#include<cstdio>
using namespace std;
const int N = 1e4 + 10;
int a[N], n;
inline int re()
{
	int x = 0;
	char c = getchar();
	bool p = 0;
	for (; c < '0' || c > '9'; c = getchar())
		p |= c == '-';
	for (; c >= '0' && c <= '9'; c = getchar())
		x = x * 10 + c - '0';
	return p ? -x : x;
}
int judge(int x)
{
	int y = x, z = 0, i;
	for (i = 2; i <= n + 1; i++)
	{
		x = a[i - 1] - y - z;
		if (x ^ 1 && x)
			return 0;
		if (!(i ^ (n + 1)) && x)
			return 0;
		z = y;
		y = x;
	}
	return 1;
}
int main()
{
	int i;
	n = re();
	for (i = 1; i <= n; i++)
		a[i] = re();
	printf("%d", judge(1) + judge(0));
	return 0;
}
原文地址:https://www.cnblogs.com/Iowa-Battleship/p/9875524.html