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

Hint


A possible solution for the sample input.

AC Code

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define inf 0x3f3f3f3f
#define maxn 1010
#define MOD 50000
using namespace std;
const double pi=acos(-1.0),ee=2.7182818284590452354;
struct node{
	int x,y;
}match[35][35];
int m,n,k,book[35][35],map[35][35],dir[4][2]={0,1,0,-1,1,0,-1,0};

bool ok(int tx,int ty)
{
	if(tx<1||tx>m||ty<1||ty>n||map[tx][ty]==1)
		return 0;
	return 1;
}

bool dfs(int x,int y)
{
	int i,j,tx,ty;
	for(i=0;i<4;i++)
	{
		tx=x+dir[i][0];
		ty=y+dir[i][1];
		if(book[tx][ty]==0&&ok(tx,ty))
		{
			book[tx][ty]=1;
			if(match[tx][ty].x+match[tx][ty].y==0||dfs(match[tx][ty].x,match[tx][ty].y))
			{
				match[tx][ty].x=x;
				match[tx][ty].y=y;
				match[x][y].x=tx;
				match[x][y].y=ty;
				return 1;
			}
		}
	}
	return 0;
}

int main(int argc, char** argv) {
	while(cin>>m>>n>>k){
		int i,j,ans=0,t=0;
		memset(map,0,sizeof(map));
		memset(match,0,sizeof(match));
		for(i=1;i<=k;i++)
		{
			int a,b;
			scanf("%d%d",&a,&b);
			map[b][a]=1;
			t++;
		}
		for(i=1;i<=m;i++)
			for(j=1;j<=n;j++)
			{
				if(map[i][j]==0&&match[i][j].x+match[i][j].y==0)
				{
					memset(book,0,sizeof(book));
					if(dfs(i,j)) ans++;
				}
			}
		if(2*ans+t==m*n) printf("YES\n");
		else printf("NO\n");
	}
		
	return 0;
}

你可能感兴趣的:(二分图)