Chessboard
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 11075 | Accepted: 3448 |
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
我们以下面这个样例为例
2 3 1 1 1
就可以得到如下图
我们把它标上号,然后在可以放置的上面连边,如 2<->4 , 3<->5 , 5<->6 等
然后我们可以求出最大匹配,下图为其中一个最大匹配
所以最大匹配为4,而图中所给的方块有N*M-K个可以放置
4≠N*M-K,所以不可行
而题中所给样例求出的最大匹配为10=N*M-K,所以可行
PS:看网上一些大牛利用单调性建图的,很巧妙,佩服。。。。。
测评成绩(POJ)
#include<cstdio>
#include<cstring>
const int N=50;
const int dx[]={0,0,1,-1};
const int dy[]={1,-1,0,0};
int n,m,k;
int total;
int hash[N][N];
int map[N*N][N*N];
int h[N*N];
int pre[N*N];
void getxy(int pos,int &x,int &y)
{
x=pos/m+1; y=pos%m;
if(y==0){y=m;x--;}
}
int getpos(int x,int y)
{
return (x-1)*m+y;
}
void read()
{
scanf("%d%d%d",&n,&m,&k);
total=n*m;
for(int i=1;i<=k;i++)
{
int x,y; scanf("%d%d",&y,&x);
hash[x][y]=true;
total--;
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(!hash[i][j])
for(int k=0;k<4;k++)
{
int x=i+dx[k];
int y=j+dy[k];
if(hash[x][y]) continue;
if(x<1||x>n) continue;
if(y<1||y>m) continue;
map[getpos(i,j)][getpos(x,y)]=true;
}
}
bool search(int x)
{
for(int i=1;i<=n*m;i++)
if(map[x][i]&&!h[i])
{
h[i]=true;
if(pre[i]==-1||search(pre[i]))
{
pre[i]=x;
//printf("%d <-> %d\n",i,x);
return true;
}
}
return false;
}
void work()
{
memset(pre,-1,sizeof(pre));
int res=0;
for(int i=1;i<=n*m;i++)
{
memset(h,0,sizeof(h));
if(search(i)) res++;
//printf("res=%d\n",res);
}
if(res==total) puts("YES");
else puts("NO");
}
int main()
{
freopen("poj2446.in","r",stdin);
freopen("poj2446.out","w",stdout);
read();
work();
return 0;
}