1088: [SCOI2005] Minesweeper Mine
Time Limit: 10 Sec Memory Limit: 162 MB[Submit][Status][Discuss]
Description
I believe everyone has played the game of minesweeper. That is, there are some mines in an n*m matrix, and you need to find out mines based on some information. When Halloween is coming
, a simple mine-sweeping game is popular in the "Yu" people. The rules of this game are the same as mine-sweeping. If a grid has no mines, the number in
it indicates the number of mines in the grid connected to it. Now the chessboard is n×2, some grids in the first column are mines, but there are no mines in the second column, as shown in the following figure:
Since the mines in the first column may have multiple solutions to meet the limit of the number of the second column, your The task is to determine how many placement
schemes there are in the first column of mines based on the information in the second column .
Input
The first row has N, and the second row has N numbers, followed by the numbers in the grid in the second column. (1<=N<=10000)
Output
A number, that is, the number of placement schemes of mines in the first column.
Sample Input
1 1
Sample Output
HINT
Source
Determine the first column to determine the entire situation, the answer is 0 or 1 or 2, and it is OK to enumerate
1 #include<bits/stdc++.h> 2 3 using namespace std; 4 5 template <typename tn> void read (tn & a) { 6 tn x = 0, f = 1; 7 char c = getchar(); 8 while (c < '0' || c > '9'){ if (c == '-') f = -1; c = getchar(); } 9 while (c >= '0' && c <= '9'){ x = x * 10 + c - '0'; c = getchar(); } 10 a = f == 1 ? x : -x; 11 } 12 13 const int MAXN = 11000; 14 int n; 15 int a[MAXN]; 16 bool f[MAXN]; 17 18 bool check() { 19 for (int i = 2; i <= n; ++i) { 20 int x = a[i - 1] - f[i - 2] - f[i - 1]; 21 if (x < 0 || x > 1) return 0; 22 f[i] = x; 23 } 24 if (a[n] == f[n] + f[n - 1]) return 1; else return 0; 25 } 26 27 int main() { 28 read(n); 29 for (int i = 1; i <= n; ++i) { 30 read(a[i]); 31 } 32 int ans = 0; 33 f[1] = 0; 34 ans += check(); 35 f[1] = 1; 36 ans += check(); 37 cout << ans << "\n"; 38 return 0; 39 }