枚举第一行第一个格子的状态(有雷或者无雷,0或1),然后根据第一个格子推出后面所有格子的状态。推出之后判断解是否可行即可。
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define REP(i,n) for(int i(0); i < (n); ++i) 6 #define rep(i,a,b) for(int i(a); i <= (b); ++i) 7 #define dec(i,a,b) for(int i(a); i >= (b); --i) 8 #define for_edge(i,x) for(int i = H[x]; i; i = X[i]) 9 10 #define LL long long 11 #define ULL unsigned long long 12 #define MP make_pair 13 #define PB push_back 14 #define FI first 15 #define SE second 16 #define INF 1 << 30 17 18 const int N = 100000 + 10; 19 const int M = 10000 + 10; 20 const int Q = 1000 + 10; 21 const int A = 30 + 1; 22 23 int a[N], b[N]; 24 int n; 25 int ans; 26 27 int main(){ 28 #ifndef ONLINE_JUDGE 29 freopen("test.txt", "r", stdin); 30 freopen("test.out", "w", stdout); 31 #endif 32 33 scanf("%d", &n); 34 rep(i, 1, n) scanf("%d", a + i); 35 rep(i, 0, 1){ 36 bool flag = true; 37 b[1] = i; 38 rep(j, 2, n) b[j] = a[j - 1] - b[j - 1] - b[j - 2]; 39 rep(j, 2, n - 1) if (b[j] < 0 || b[j] > 1){ flag = false; break;} 40 if (b[n] < 0 || b[n] > 1) flag = false; 41 rep(j, 1, n) if (b[j - 1] + b[j] + b[j + 1] != a[j]){ flag = false; break;} 42 //rep(j, 1, n) printf("%d ", b[j]); putchar(10); 43 44 if (flag) ++ans; 45 } 46 47 printf("%d ", ans); 48 49 50 51 return 0; 52 53 }