Chessboard
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 12811 | Accepted: 4005 |
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
使用1*2的小长方块覆盖n*m的区域,其中给出的t个点不能覆盖,问存不存该可能
二分匹配的题目,建图是最关键的,1*2的小方块会覆盖住两个方格,所以相邻的方块一定是在不同的点集中,我用的m[][]数组记录n*m,用bfs来分配点,得到左右点集,如果左右点集中的点相邻,就意味着他们之间有一条线,对这个图做二分匹配,求出最大匹配,如果存在由1*2的方块覆盖整体面积,那么就会有 n*m-t = 最大匹配数 * 2 ,否则不存在全部覆盖的可能
#include <stdio.h>
#include <string.h>
#include <math.h>
int p[40][40] ;//0是空白点,-1不可选点,1左点集,2右点集
struct node1
{
int x , y ;
} l[1600];
struct node2
{
int x , y ;
} r[1600];
struct node
{
int x , y ;
} d[1600] ;
int nl , nr , n , m ;
int link[1600] , temp[1600] ;
int f(int x)
{
int i , j ;
for(i = 0 ; i < nr ; i++)
{
if( temp[i] == 0 && fabs(l[x].x - r[i].x) + fabs( l[x].y - r[i].y ) == 1 )
{
temp[i] = 1 ;
if( link[i] == -1 || f( link[i] ) )
{
link[i] = x ;
return 1 ;
}
}
}
return 0 ;
}
int dd[][2] = { {0,-1},{0,1},{1,0},{-1,0} } ;
void bfs(int i,int j)
{
int low = 0 , top = 1 , u , v ;
d[0].x = i ;
d[0].y = j ;
p[i][j] = 1 ;
while(low < top)
{
node mid = d[low++] ;
for(i = 0 ; i < 4 ; i++)
{
u = mid.x + dd[i][0] ;
v = mid.y + dd[i][1] ;
if(u > 0 && u <= n && v > 0 && v <= m && p[u][v] == 0 )
{
if( p[mid.x][mid.y] == 1 )
p[u][v] = 2 ;
else
p[u][v] = 1 ;
d[top].x = u ;
d[top++].y = v ;
}
}
}
}
int main()
{
int t , tt , i , j , ans ;
while(scanf("%d %d %d", &n, &m, &t)!=EOF)
{
tt = t ;
memset(p,0,sizeof(p));
memset(link,-1,sizeof(link));
nl = 0 ;
nr = 0 ;
ans = 0 ;
while(t--)
{
scanf("%d %d", &j, &i);
p[i][j] = -1 ;
}
if( (n*m-tt)%2 )
{
printf("NO\n");
continue ;
}
for(i = 1 ; i <= n ; i++)
for(j = 1 ; j <= m ; j++)
{
if( p[i][j] == 0 )
{
bfs(i,j);
}
}
for(i = 1 ; i <= n ; i++)
for(j = 1 ; j <= m ; j++)
{
if(p[i][j] == 1)
{
l[nl].x = i ;
l[nl++].y = j ;
}
else if(p[i][j] == 2)
{
r[nr].x = i ;
r[nr++].y = j ;
}
}
for(i = 0 ; i < nl ; i++)
{
memset(temp,0,sizeof(temp));
if( f(i) )
{
ans++ ;
}
}
if( (n*m-tt) == ans*2 )
printf("YES\n");
else
printf("NO\n");
}
return 0;
}