P1193Minesweeper accepted Tags: [show tags]<textarea id="code" class="textbox" style=""></textarea>Describe
I believe everyone has played the game of minesweeper. There are some thunder in a n*n matrix, you need to find out the ray based on some information. Halloween arrived, "Yu" has been a simple game of minesweeper, the rules of the game and the same as mine, if a lattice does not have thunder, then it is the number of the figure and his 8 connected lattice inside the number of thunder. Now the chessboard is n*2, some grids in the first column are thunder, and the second column has no thunder, such as:
o 1
* 2
* 3
* 2
o 2
* 2
* 2 (' * ' stands for thunder, ' O ' stands for no thunder)
Since the first category of mines may have multiple scenarios that meet the limits of the number of second columns, your task is to find out how many of the first Ray's options are placed according to the information in the second column.
Format input Format
The first behavior n, the second row has n number, in order the number of the second column in the lattice. (1<=n<=10000)
Output format
A number, that is, the number of thunder in the first column.
Example 1 sample input 1[copy]
21 1
Sample output 1[Copy]
2
Limit
1s
Source
NOIP2006 Summer Camp
Set DP[I][J] Indicates the number of the three squares that he covers (1 of the binary of J has been released by Ray)
So dp[n][i] = Dp[n-1][j] {(i &3) = = ((J & 6) >> 1)}
Note that you must remove the bag, otherwise it will be very miserable.
(Hint: The problem of state compression is generally converted to binary processing, of course, can be directly extended array, but it is not convenient)
Package Ds;import java.util.*;import java.math.*;import java.io.*;p ublic class main{static Scanner cin;static PrintStream cout;static int maxn = (int) 1E4 + 5; static int [] MP = new Int[maxn];static int [] DP = new int[maxn][10];static int bit_count (int val) {int ret = 0;while (VA L > 0) {val &= (VAL-1); ret + +;} return ret;} public static void Main (String [] agrs) throws ioexception{cin = new Scanner (new Bufferedinputstream (system.in)); cout = new PrintStream (System.out), while (Cin.hasnext ()) {int N = Cin.nextint (), for (int i = 1;i <= N; i + +) {Mp[i] = Cin.nextint ();} for (int i = 0;i <= N + 1;i + +) {Arrays.fill (Dp[i], 0);} if (mp[1] = = 0) {dp[1][0] = 1;} else if (mp[1] = = 1) {dp[1][2 << 1] = 1;dp[1][1 << 1] = 1;} else if (mp[1] = = 2) {dp[1][3 << 1] = 1;} for (int i = 2;i <= N; i + +) {for (int j = 0;j <= 7;j + +) {for (int k = 0;k <= 7;k + +) {int c = (J & 3); if (c = = (k >> 1) && bit_count (j) = = Mp[i]) {Dp[i][j] + dp[i-1][k];}}} int ans = 0;for (int k =0;k <= 3;k + +) {if (Bit_count (k) = = Mp[n]) ans + = dp[n][k];} Cout.println (ANS);}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
P1193 Minesweeper (DP State compression)