Chessboard
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 11700 | Accepted: 3653 |
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.
Source
POJ Monthly,charlescpp
题意:
给出一个m*n的棋盘,其中有k个洞,要求用1*2的木块去盖住整个棋盘,规定木板不能重叠,而且洞口上面不能有木板。若能用木板将所有非洞处覆盖,则输出YES;反之则输出NO。
代码:
#include<cstdio>
#include<cstring>
#define N 150
#define MAX 2000
bool map[MAX][MAX];
bool vist[MAX];
int pre[MAX];
int mat[N][N];
int m,n,k;
int tm,tn;
bool find(int cur)
{
for(int i=1;i<=tn;i++)
{
if(map[cur][i] && !vist[i])
{
vist[i]=true;
if(pre[i]==-1 || find(pre[i]))
{
pre[i]=cur;
return true;
}
}
}
return false;
}
int main()
{
int x,y;
while(scanf("%d%d%d",&m,&n,&k)!=EOF)
{
memset(mat,0,sizeof(mat));
memset(map,false,sizeof(map));
memset(pre,-1,sizeof(pre));
if((m*n-k)%2!=0)
{
printf("NO\n");
return 0;
}
for(int i=1;i<=k;i++)
{
scanf("%d%d",&x,&y);
mat[y][x]=-1;
}
int j;
tn=0;
for(int i=1;i<=m;i++)
{
if(i%2==0) j=1;
else j=2;
for(;j<=n;j+=2)
if(mat[i][j]!=-1)
mat[i][j]=++tn;
}
tm=0;
for(int i=1;i<=m;i++)
{
if(i%2==0) j=2;
else j=1;
for(;j<=n;j+=2)
if(mat[i][j]!=-1)
{
mat[i][j]=++tm;
if(j>1 && mat[i][j-1]!=-1)
map[mat[i][j]][mat[i][j-1]]=true;
if(j<n && mat[i][j+1]!=-1)
map[mat[i][j]][mat[i][j+1]]=true;
if(i>1 && mat[i-1][j]!=-1)
map[mat[i][j]][mat[i-1][j]]=true;
if(i<m && mat[i+1][j]!=-1)
map[mat[i][j]][mat[i+1][j]]=true;
}
}
int ans=0;
for(int i=1;i<=tm;i++)
{
memset(vist,false,sizeof(vist));
if(find(i)) ans++;
}
if((m*n-k)/2==ans) printf("YES\n");
else printf("NO\n");
}
return 0;
}