[SCOI2005]扫雷
这道题一开始尝试用数学方程来解,后来觉得太复杂,事实上解的个数很少,所以就考虑搜索了,不确定的格子枚举,确定的格子填上,如果发现错误就退出。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 6 using namespace std; 7 8 #define re register 9 #define rep(i, a, b) for (re int i = a; i <= b; ++i) 10 #define repd(i, a, b) for (re int i = a; i >= b; --i) 11 #define maxx(a, b) a = max(a, b); 12 #define minn(a, b) a = min(a, b); 13 #define LL long long 14 #define inf (1 << 30) 15 16 inline int read() { 17 int w = 0, f = 1; char c = getchar(); 18 while (!isdigit(c)) f = c == '-' ? -1 : f, c = getchar(); 19 while (isdigit(c)) w = (w << 3) + (w << 1) + (c ^ '0'), c = getchar(); 20 return w * f; 21 } 22 23 const int maxn = 1e4 + 5; 24 25 int n, a[maxn], b[maxn], ans = 0; 26 27 void dfs(int x) { 28 if (x == n+1 && a[x-1] == b[x-2] + b[x-1]) { 29 ans++; 30 return; 31 } 32 if (x > 1) { 33 if (a[x-1] == b[x-1] + b[x-2]) { 34 b[x] = 0; 35 dfs(x+1); 36 } 37 else if (a[x-1] == b[x-1] + b[x-2] + 1) { 38 b[x] = 1; 39 dfs(x+1); 40 } 41 else return; 42 } else { 43 b[x] = 0; 44 dfs(x+1); 45 b[x] = 1; 46 dfs(x+1); 47 } 48 } 49 50 int main() { 51 n = read(); 52 53 rep(i, 1, n) a[i] = read(); 54 55 dfs(1); 56 57 printf("%d", ans); 58 59 return 0; 60 }