首页 > 代码库 > POJ——T2446 Chessboard

POJ——T2446 Chessboard

http://poj.org/problem?id=2446

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 18560 Accepted: 5857

Description

Alice and Bob often play games on chessboard. One day, Alice draws a board with size M * N. She wants Bob to use a lot of cards with size 1 * 2 to cover the board. However, she thinks it too easy to bob, so she makes some holes on the board (as shown in the figure below). 
技术分享

We call a grid, which doesn’t contain a hole, a normal grid. Bob has to follow the rules below: 
1. Any normal grid should be covered with exactly one card. 
2. One card should cover exactly 2 normal adjacent grids. 

Some examples are given in the figures below: 
技术分享 
A VALID solution.

技术分享 
An invalid solution, because the hole of red color is covered with a card.

技术分享 
An invalid solution, because there exists a grid, which is not covered.

Your task is to help Bob to decide whether or not the chessboard can be covered according to the rules above.

Input

There are 3 integers in the first line: m, n, k (0 < m, n <= 32, 0 <= K < m * n), the number of rows, column and holes. In the next k lines, there is a pair of integers (x, y) in each line, which represents a hole in the y-th row, the x-th column.

Output

If the board can be covered, output "YES". Otherwise, output "NO".

Sample Input

4 3 22 13 3

Sample Output

YES

Hint

技术分享 
A possible solution for the sample input.

Source

POJ Monthly,charlescpp
 
题解:
技术分享把棋盘染成这个样子,有障碍的不染,用黑色格子与白色格子匹配,对这一个二分图求最大匹配。如果Ans*2+K=N*M,则能完全覆盖

 

 1 #include <algorithm> 2 #include <cstring> 3 #include <cstdio> 4  5 using namespace std; 6  7 const int N(32*32); 8 int n,m,p,x,y,ans; 9 int match[N][N][2],lose[N][N];10 int vis[N][N],sumvis;11 int fx[4]={1,0,-1,0};12 int fy[4]={0,1,0,-1};13 14 bool DFS(int x,int y)15 {16     for(int i=0;i<4;i++)17     {18         int xx=x+fx[i], yy=y+fy[i];19         if(vis[xx][yy]!=sumvis&&!lose[xx][yy])20         {21             vis[xx][yy]=sumvis;22             if(!match[xx][yy][0]||DFS(match[xx][yy][0],match[xx][yy][1]))23             {24                 match[xx][yy][0]=x;25                 match[xx][yy][1]=y;26                 return true;27             }28         }29     }30     return false;31 }32 33 int main()34 {35     while(~scanf("%d%d%d",&n,&m,&p))36     {37         if((n*m-p)%2)38         {39             printf("NO\n");40             continue;41         }42         ans=sumvis=0;43         memset(vis,0,sizeof(vis));44         memset(lose,0,sizeof(lose));45         memset(match,0,sizeof(match));46         for(int i=1;i<=p;i++)47         {48             scanf("%d%d",&x,&y);49             lose[y][x]=1;50         }51         for(int i=0;i<=n;i++) 52             lose[i][0]=lose[i][m+1]=1;53         for(int i=0;i<=m;i++) 54             lose[0][i]=lose[n+1][i]=1;55         for(int i=1;i<=n;i++)56           for(int j=1;j<=m;j++)57             if((i+j)%2==0&&!lose[i][j])58             {59                 sumvis++;60                 if(DFS(i,j)) ans++;61             }62         if(ans*2+p==m*n) printf("YES\n");63         else printf("NO\n");64     }65     return 0;66 }

 

POJ——T2446 Chessboard