poj Chessboard 2446 (最大匹配&转换)

Chessboard
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 15901 Accepted: 4926

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.

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.
//题意:
给你一个m*n的矩形,现在在这个矩形上有k个洞,告诉你这k个洞的坐标位置,问是否可以用1*2的矩形纸片将除了洞的其他的位置都遮住,并且1*2的小矩形不能有相互遮盖。
//思路:

把这个棋盘看成一个类似国际象棋的棋盘,黑白相间的那种。任何2个相邻的格子肯定是不同颜色的(黑和白)。经过研究发现,如果一个格子的行号和列号加起来为奇数,那与它相邻的格子的行号和列号加起来一定是偶数,如果一个格子的行号和列号加起来为偶数,那与它相邻的格子的行号和列号加起来一定是奇数。

这样,我们就可以把这张棋盘分为奇数集合和偶数集合。加边的时候,我们只需要加它上面或左面就可以了。。

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#define INF 0x3f3f3f3f
#define IN __int64
#define ull unsigned long long
#define ll long long
#define N 1100
#define M 1000000007
using namespace std;
int head[N],next[N*N],key[N*N],top;
int y[N];
int vis[N];
int map[N][N];
int m,n,k;
void add(int u,int v)
{
	key[top]=v;
	next[top]=head[u];
	head[u]=top++;
}
int dfs(int u)
{
	int v;
	for(int i=head[u];i!=-1;i=next[i])
	{
		v=key[i];
		if(!vis[v])
		{
			vis[v]=1;
			if(y[v]==-1||dfs(y[v]))
			{
				y[v]=u;
				return 1;
			}
		}
	}
	return 0;
}
int main()
{
	int xx,yy;
	while(scanf("%d%d%d",&m,&n,&k)!=EOF)
	{
		if((m*n-k)&1)
		{
			printf("NO\n");
			continue;
		}
		top=0;
		memset(head,-1,sizeof(head));
		memset(y,-1,sizeof(y));
		memset(map,1,sizeof(map));
		for(int i=0;i<k;i++)
		{
			scanf("%d%d",&yy,&xx);
			map[xx-1][yy-1]=0;
		}
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<n;j++)
			{
				if(map[i][j])
				{
					if(i-1>=0&&map[i-1][j])//上下方向连接 
					{
						if((i+j)&1)//从下到上 
							add(i*n+j,(i-1)*n+j);
						else//从上到下
							add((i-1)*n+j,i*n+j); 
					}
					if(j-1>=0&&map[i][j-1])//左右方向连接 
					{
						if((i+j)&1)//从右向左 
							add(i*n+j,i*n+j-1);
						else//从左向右 
							add(i*n+j-1,i*n+j);
					}
				}
			}
		}
		int sum=0;
		for(int i=0;i<n*m;i++)
		{
			memset(vis,0,sizeof(vis));
			if(dfs(i))
				sum++;
		}
		printf(sum==((m*n-k)/2)?"YES":"NO");
	}
	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值