POJ2446--Chessboard

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
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 38*38
bool vis[maxn],map[38][38];
int first[maxn],marry[maxn];
int vv[maxn*4],nxt[maxn*4];
int e,m,n,k;
int heng[] = {0,0,1,-1};
int zong[] = {1,-1,0,0};
void addEdge(int u,int v)
{
	vv[e] = v;	nxt[e] = first[u];	first[u] = e++;
}
void createmap()
{
	for(int i = 1;i <= m;i++)
	{
		for(int j = 1;j <= n;j++)
		{
			if(map[i][j])		continue;
			for(int k = 0;k < 4;k++)
			{
				if(!map[i+heng[k]][j+zong[k]])
				{
					addEdge((i-1)*n+j,(i+heng[k]-1)*n+j+zong[k]);
				}
			}
		}
	}
}
bool dfs(int u)
{
	for(int i = first[u];i != -1;i = nxt[i])
	{
		int v = vv[i];
		if(!vis[v])
		{
			vis[v] = 1;
			if(!marry[v] || dfs(marry[v]))
			{
				marry[u] = v;
				marry[v] = u;
				return 1;
			}
		}
	}
	return 0;
}
int main()
{
	//freopen("in.txt","r",stdin);
	while(scanf("%d%d%d",&m,&n,&k)==3)
	{
		memset(first,-1,sizeof(first));
		memset(map,0,sizeof(map));
		memset(marry,0,sizeof(marry));
		for(int i = 0;i <= n+1;i++)
		{
			map[0][i] = map[m+1][i] = 1;
		}
		for(int i = 0;i <= m+1;i++)
		{
			map[i][0] = map[i][n+1] = 1;
		}//这两个for循环是将地图封闭起来。
		for(int i = 1;i <= k;i++)
		{
			int x,y;
			scanf("%d%d",&x,&y);
			map[y][x] = 1;
		}
		e = 0;
		createmap();
		int ans = 0;
		for(int i = 1;i <= n*m;i++)
		{
			memset(vis,0,sizeof(vis));
			vis[i] = 1;
			if(!marry[i] && dfs(i))	ans++;
		}
		if(ans*2+k == n*m)	printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值