Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 13176   Accepted: 4118

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.

给你一个棋盘,棋盘上有几个洞。要求你用一些1*2的卡片覆盖没有洞的区域,一个格子仅仅能有一张卡片覆盖。

看能否恰好覆盖。





二分匹配:利用二分匹配。两个能匹配的格子的坐标和必定奇偶性不同。利用这一点能够降低时间耗费。

#include"stdio.h"
#include"string.h"
#include"queue"
using namespace std;
#define N 35
#define M 1200
int g[N][N],n,m;
int dir[4][2]={0,1,0,-1,-1,0,1,0};
int mark[M],link[M];
int judge(int x,int y)
{
if(x>=0&&x<n&&y>=0&&y<m)
return 1;
return 0;
}
int find(int k)
{
int i,j,x,y,di,dj;
x=k/m;
y=k%m;
for(i=0;i<4;i++)
{
di=dir[i][0]+x;
dj=dir[i][1]+y;
if(judge(di,dj)&&!g[di][dj])
{
j=di*m+dj;
if(!mark[j])
{
mark[j]=1;
if(link[j]==-1||find(link[j]))
{
link[j]=k;
return 1;
}
}
}
}
return 0;
}
int main()
{
int u,v,k,i,j;
while(scanf("%d%d%d",&n,&m,&k)!=-1)
{
memset(g,0,sizeof(g));
for(i=0;i<k;i++)
{
scanf("%d%d",&v,&u);
u--;v--;
g[u][v]=1;
}
if((n*m-k)&1)
{
printf("NO\n");
continue;
}
int ans=0;
memset(link,-1,sizeof(link));
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if((i+j)%2==0||g[i][j]) //(i+j)奇偶性! !!
continue;
memset(mark,0,sizeof(mark));
ans+=find(i*m+j);
}
}
//printf("%d\n",ans);
if(ans*2==n*m-k)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}

poj 2446 Chessboard (二分图利用奇偶性匹配)的更多相关文章

  1. POJ 2446 Chessboard (二分图匹配)

    题意 在一个N*M的矩形里,用1*2的骨牌去覆盖该矩形,每个骨牌只能覆盖相邻的两个格子,问是否能把每个格子都盖住.PS:有K个孔不用覆盖. 思路 容易发现,棋盘上坐标和为奇数的点只会和坐标和为偶数的点 ...

  2. POJ 2446 Chessboard (二分图最大匹配)

    题目链接:http://poj.org/problem?id=2446 给你一个n*m的棋盘,其中有k个洞,现在有1*2大小的纸片,纸片不能覆盖洞,并且每个格子最多只能被覆盖一次.问你除了洞口之外这个 ...

  3. poj 2446 Chessboard (二分匹配)

    Chessboard Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12800   Accepted: 4000 Descr ...

  4. POJ 2446 Chessboard【二分图最大匹配】

    <题目链接> 题目大意: 给你一个n*m的棋盘,其中有k个洞,现在有1*2大小的纸片,纸片不能覆盖洞,并且每个格子最多只能被覆盖一次.问你除了洞口之外这个棋盘是否能被纸片填满. 解题分析: ...

  5. POJ 2446 Chessboard(二分图最大匹配)

    题意: M*N的棋盘,规定其中有K个格子不能放任何东西.(即不能被覆盖) 每一张牌的形状都是1*2,问这个棋盘能否被牌完全覆盖(K个格子除外) 思路: M.N很小,把每一个可以覆盖的格子都离散成一个个 ...

  6. POJ 2446 Chessboard

    要求用占两格的长方形铺满平面上除去指定点 二分图匹配 #include <iostream> #include <cstdio> #include <cstring> ...

  7. poj 2195 二分图最优匹配 或 最小费用最大流

    就是最基本的二分图最优匹配,将每个人向每个房子建一条边,权值就是他们manhattan距离.然后对所有权值取反,求一次最大二分图最优匹配,在将结果取反就行了. #include<iostream ...

  8. [模板] 匈牙利算法&&二分图最小字典序匹配

    匈牙利算法 简介 匈牙利算法是一种求二分图最大匹配的算法. 时间复杂度: 邻接表/前向星: \(O(n * m)\), 邻接矩阵: \(O(n^3)\). 空间复杂度: 邻接表/前向星: \(O(n ...

  9. UVa 11383 少林决胜(二分图最佳完美匹配)

    https://vjudge.net/problem/UVA-11383 题意: 给定一个N×N矩阵,每个格子里都有一个正整数W(i,j).你的任务是给每行确定一个整数row(i),每列也确定一个整数 ...

随机推荐

  1. .Net高级进阶,在复杂的业务逻辑下,如何以最简练的代码,最直观的编写事务代码?

    本文将通过场景例子演示,来通俗易懂的讲解在复杂的业务逻辑下,如何以最简练的代码,最直观的编写事务代码. 通过一系列优化最终达到两个效果,1.通过代码块来控制事务(分布式事务),2.通过委托优化Tran ...

  2. POI不同版本替换Word模板时的问题

    一.问题描述 通过POI,把Word中的占位符替换为实际的值,以生成复杂结构的业务报告. 在POI 3.9上,功能正常.由于某些原因升级到POI 3.10.1后,项目组反馈说Word模板出错,无法生成 ...

  3. RPA(Robotic Process Automation)的概要介绍

    最近因为公司业务的需要,开始关注RPA的内容,奈何国内相关的信息太少,只能硬着头皮啃英文了. 下面记录的内容作为学习笔记,有不对的地方请大家指教. 首先RPA(Robotic Process Auto ...

  4. 【Tesseract】Tesseract API在VS 2013中的配置以及调用

    想要在VS中使用Tesseract库,必须使用经过相对应的VS版本编译过的dll以及lib.比如在VS 2013中,就必须使用在VS 2013中编译过的Tesseract库. 这里我给出经过VS 20 ...

  5. mysql 运维常见操作

    初始安装并赋予密码:   [root@Alinx html]# yum install -y mysql mysql-server                         #安装mysql可与 ...

  6. C#移位运算(左移和右移)

    C#是用<<(左移) 和 >>(右移) 运算符是用来执行移位运算. 左移 (<<) 将第一个操作数向左移动第二个操作数指定的位数,空出的位置补0.  左移相当于乘. ...

  7. C#中获取当前系统中安装的所有字体及预定义颜色

    需要引用命名空间using System.Drawing.Text;.... //获取系统字体:InstalledFontCollection fc = new InstalledFontCollec ...

  8. Bean property属性说明

                                  来自为知笔记(Wiz)

  9. Ant的使用

    Ant的使用 什么是Apache Ant Apache Ant是一个基于java的软件构建工具(build tool),理论上它有点类似C/C++的make工具 为什么要用ant? make, gnu ...

  10. vimgdb安装

    vimgdb install ************** a) You need: vim-7.3.tar.bz2 http://www.vim.org/sources.php vimgdb- (t ...