HDU 5965 扫雷 题解
题目大意
有一个 \(3\times n\) 的扫雷区域,中间一行没有雷,并且会给出周围 \(8\) 个位置的雷的个数(和扫雷游戏规则一致),问合法的雷的放置方案一共有多少?
题解
我们设给出的雷的情况是 \(a_n\),我们假设第 \(i\) 个位置(竖着看)放的雷有 \(b_i(0\leq b_i\leq 2)\) 个,则如果第一列的情况确定,则后边的每一个地方的情况就已经确定下来了,所以我们枚举第一列的情况,每次计算答案即可。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| #include <cstdio> #include <cctype> #include <algorithm> #include <cstring> #include <iostream> #include <cassert> #include <cmath> using namespace std; typedef long long ll; template <typename _Tp> void read(_Tp &a, char c = 0, int f = 1) { for(c = getchar(); !isdigit(c); c = getchar()) if(c == '-') f = -1; for(a = 0; isdigit(c); a = a * 10 + c - '0', c = getchar()); a *= f; } template <typename _Tp> void write(_Tp a) { if(a < 0) putchar('-'), a = -a; if(a > 9) write(a / 10); putchar(a % 10 + '0'); }
const int N = 1e4 + 5; const int MOD = 1e8 + 7; char s[N]; int n, a[N], b[N];
int main() { int T; read(T); while(T--) { scanf("%s", s + 1); n = strlen(s + 1); int ans = 0; for(int i = 1; i <= n; i++) a[i] = s[i] - '0'; for(int st = 0; st <= 2; st++) { b[1] = st; if(b[1] > a[1]) break; bool f = 1; for(int i = 2; i <= n; i++) { int c = a[i - 1] - b[i - 1] - b[i - 2]; if(c >= 3 || c < 0) { f = 0; break; } b[i] = c; } if(f && b[n - 1] + b[n] == a[n]) { int __ans = 1; for(int i = 1; i <= n; i++) { if(b[i] == 1) { __ans = (__ans * 2) % MOD; } } ans = (ans + __ans) % MOD; } } write(ans), putchar('\n'); } return 0; }
|