题目:http://poj.org/problem?id=2446
Chessboard
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 15250 | Accepted: 4751 |
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.
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 2 2 1 3 3
Sample Output
YES
Hint
A possible solution for the sample input.
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N=2000;
bool bmap[N][N],bmask[N],tag[35][35];
int nx,ny,cx[N],cy[N];
int find(int u){
for(int j=1;j<=ny;j++){
if(bmap[u][j]&&!bmask[j]){
bmask[j]=1;
if(cy[j]==-1||find(cy[j])){
cy[j]=u;
cx[u]=j;
return 1;
}
}
}
return 0;
}
int maxmatch(){
int res=0;
memset(cx,-1,sizeof(cx));
memset(cy,-1,sizeof(cy));
for(int i=1;i<=nx;i++){
if(cx[i]==-1){
for(int j=1;j<=ny;j++) bmask[j]=0;
res+=find(i);
}
}
return res;
}
int dir[4][2]={{0,-1},{0,1},{-1,0},{1,0}};
int main()
{
//freopen("cin.txt","r",stdin);
int m,n,k;
while(cin>>m>>n>>k){
int a,b;
nx=ny=n*m;
memset(tag,0,sizeof(tag));
memset(bmap,0,sizeof(bmap));
for(int i=0;i<k;i++){
scanf("%d%d",&b,&a);
tag[a][b]=1;
}
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
if(tag[i][j]==1) continue;
int x,y;
for(int k=0;k<4;k++){
x=i+dir[k][0];
y=j+dir[k][1];
if(x<=m&&x>=1&&y<=n&&y>=1&&tag[x][y]==0){
a=(i-1)*n+j;
b=(x-1)*n+y;
bmap[a][b]=1;
}
}
}
}
int res=maxmatch();
//cout<<res<<endl;
if(res==m*n-k) puts("YES");
else puts("NO");
}
return 0;
}