Acdream 1132 Chess (pressure DP)

Source: Internet
Author: User

ChessTime limit:2000/1000ms (java/others)Memory limit:128000/64000kb (java/others)Submit statistic Next Problemproblem Description

Xiang-qi is a kind of traditional Chinese chess. There is a special role in the game called "Jiang" (or called "Shuai"). When you want to operate this kind of role, it can only be dominating the four neighbor cell and can of course attack the Rol E There (in fact there was a special condition can allow the role attack farther distance but we ignore this condition).

Now we have a n*n chessboard, we want to place some "Jiang" on it. Of course we have several restraints on placing chess pieces:

1. The placing chess role cannot dominate each of the other.
2. The chessboard all cells without placing chess should is dominated.
3. There is some cells called "Hole" which cannot is placed by chess piece. Attention that the hole should also is dominated.

For a given chessboard, we want to know the least number of chess pieces to place which can satisfy the restraints above.

Input

There is multiple test cases.

In each test case, the first line contains the integers N and K indicating the size of the chessboard and the number of th e holes on the chessboard. (1 <= N <= 9, 0 <= K <= n*n)

The next K line contains integers x and y indicating the cell (x, y) is a hole cell. (1 <= x, y <= N)

You can get more details from the sample and hint.

Output

For each test case, you should output an integer indicating the answer of the test case. If There is no-i-chess pieces, please output-1.

Sample Input
3 21 13 3
Sample Output
3
Hint

In the sample we can have several ways to placing the chess pieces:

Place on (1, 3), (2, 1) and (3, 2);
Place on (3, 1), (1, 2) and (2, 3);

Although place on (1, 2), (2, 2) and (3, 2) can dominate all cell but it's not satisfy the first restraint. and place on (1, 1), (1, 3) and (3, 2) are also illegal because the cell (1, 1) is a hole.


Title Link: http://acdream.info/problem?pid=1132



Dp[i][cur][up] Represents the i row Type[cur] state, i-1 Line is type[up] when the number of pieces//ideas: pre-processing which states there is no adjacent 1, the status of 1 is put,//because the current state of the first row I can not drink hole conflict, And can not drink i-1 line State conflict, and must make the I-1 row in the grid to be controlled,//and I-1 row is controlled by the state and i-2 also related, so also to record the state of I-2 line, so with 3-dimensional dp#include<iostream> #include < cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <vector> #include <set> #include <map> #define L (x) (x<<1) #define R (x) (x  <<1|1) #define MID (x, y) ((x+y) >>1) #define EPS 1e-8//typedef __int64 ll; #define FRE (i,a,b) for (i = A; I < b; i++) #define Frer (i,a,b) for (i = A; i > =b;i--) #define MEM (T, v) memset ((t), V, sizeof (t)) #define SSF (n) scanf ("%s", N) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d", &a, &b) #define SFFF (A,B,C) scan F ("%d%d", &a, &b, &c) #define PF printf#define Bug pf ("hi\n") using namespace std; #define I NF 0x3f3f3f3f#define N 520int dp[10][n][n];int type[n],k;int n,m,h[10],num[n];int lowbit (int x)//calculates the number of X state 1 {int t=0;while (x) {t++;x&= (x-1);} return t;} bool OK (int x) {if (x& (x<<1)) return False;return true;} void Inint ()//preprocessing each row does not have an adjacent 11 state {int I,j;int len=1<<n;k=0;fre (i,0,len) if (ok (i)) Type[k++]=i;fre (i,0,k) num[i]=l    Owbit (Type[i]);     Find out the number of 1 of this state}void solve () {int i,j,cur,up,uup;          Fre (i,2,n) fre (cur,0,k)//Current status {if (Type[cur]&h[i]) continue;            FRE (UP,0,K)//Previous line status {if (type[up]&h[i-1]) continue;            if (Type[up]&type[cur]) continue;                FRE (UUP,0,K)//on the row state {if (type[uup]&h[i-2]) continue;                  if (Type[uup]&type[up]) continue;                if (!dp[i-1][up][uup]) continue; int t=type[up]| (type[up]<<1) |                (type[up]>>1);                T|=TYPE[UUP];                T|=type[cur];    T&= (1<<n)-1; This step is important, bug half-day if (t!= (1&Lt;<n)-1) continue;            Dp[i][cur][up]=min (Dp[i][cur][up],dp[i-1][up][uup]+num[cur]); }}}int Ans=inf;fre (i,0,k) fre (up,0,k) {if (dp[n][i][up]==inf) continue; if (h[n]&type[i]) continue; I F (H[n-1]&type[up]) continue; if (Type[i]&type[k]) continue; int t=type[i]| (type[i]<<1) | (type[i]>>1); T|=type[up]; T&= (1<<n)-1; if (t!= (1<<n)-1) continue; if (!dp[n][i][up]) continue; Ans=min (Ans,dp[n][i][up]);     } if (Ans==inf) ans=-1; printf ("%d\n", ans);}    int main () {int i,j;       while (~SFF (n,m)) {inint ();       MEM (h,0);       int x, y;     while (m--) {SFF (x, y);   h[x]|=1<< (y-1);       } mem (dp,0);         Fre (i,0,k) {if (type[i]&h[1]) continue;         Dp[1][i][0]=num[i]; }solve ();} return 0;}


Acdream 1132 Chess (pressure DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.