Chessboard
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 12277 | Accepted: 3823 |
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
题意是说n*m的棋盘,其中有k个洞,现在给你1*2的纸片,如果能够全部覆盖棋盘且不覆盖洞,而且棋盘的每个格子都被覆盖一次,就输出YES,否则输出NO。
这都能和二分匹配联系起来,真是看不出来啊。
把这个棋盘看成是黑白相间的棋盘,任何相邻的两个格子肯定不是同一种颜色的。如果一个格子的行数和列数加起来为奇数(偶数)那么与它相邻的格子的行数和列数加起来肯定是偶数(奇数)。那么这样的话就可以把整个棋盘分为奇数集合和偶数集合。然后在加边的时候,往上加或者往左加就可以了。
//4364K 47MS
#include<stdio.h>
#include<string.h>
#define M 2007
int link[M],head[M],node;
bool vis[M],map[M][M];
struct E
{
int v,next;
}edg[M*M];
int n,m,k;
void addedge(int u,int v)
{
edg[node].v=v;
edg[node].next=head[u];
head[u]=node++;
}
bool find(int u)
{
for(int i=head[u];i!=-1;i=edg[i].next)
{
int v=edg[i].v;
if(!vis[v])
{
vis[v]=true;
if(!link[v]||find(link[v]))
{
link[v]=u;
return true;
}
}
}
return false;
}
int main()
{
while(scanf("%d%d%d",&n,&m,&k)!=EOF)
{
memset(map,true,sizeof(map));
memset(link,0,sizeof(link));
memset(head,-1,sizeof(head));
if((n*m-k)&1){printf("NO\n");continue;}
int a,b,count=0;
node=0;
for(int i=0;i<k;i++)
{
scanf("%d%d",&b,&a);
map[a-1][b-1]=false;
}
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(map[i][j])
{
if(i-1>=0&&map[i-1][j])
{
if((i+j)&1)addedge(i*m+j,(i-1)*m+j);
else addedge((i-1)*m+j,i*m+j);
}
if(j-1>=0&&map[i][j-1])
{
if((i+j)&1)addedge(i*m+j,i*m+j-1);
else addedge(i*m+j-1,i*m+j);
}
}
}
for(int i=0;i<n*m;i++)
{
memset(vis,false,sizeof(vis));
if(find(i))count++;
}
//printf("coutn==%d\n",count);
if(count==(n*m-k)/2)printf("YES\n");
else printf("NO\n");
}
return 0;
}