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. Struts2知识整理

    准备找工作了.好忐忑!!! 整理整理知识,好好准备. 其实现在Struts2好像不是特别流行,不过还是有用武之地的. struts2简介 struts2是基于mvc开发模型的框架,属于表现层框架 核心 ...

  2. javascript 中parseInt 的用法

    <!DOCTYPE html> <html> <head lang="en">     <meta charset="UTF-8 ...

  3. maven项目导出依赖的Jar包以及项目本身以jar包形式导出详细教程

    一.maven项目已jar包形式导出 1.首先右键项目,选择Export 2.选择好项目,设置导出路径和jar名字即可: 二.导出maven项目所依赖的所有jar包 1.右键项目,选择Export 2 ...

  4. Linux系列教程(二十一)——Linux的bash基本功能

    上篇博客我们介绍了什么是shell,以及编写shell脚本的两种执行方式.我们知道在敲命令的时候,有很多快捷键,比如tab键能补全命令,在比如为什么我们直接敲 ll 命令能显示目录的长格式,其实这是b ...

  5. SpringMVC注解HelloWorld

    今天整理一下SpringMVC注解 欢迎拍砖 @RequestMapping RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上.用于类上,表示类中的所有响应请求的方法都是 ...

  6. python中print()函数的“,”与java中System.out.print()函数中的“+”

    python中的print()函数和java中的System.out.print()函数都有着打印字符串的功能. python中: print("hello,world!") 输出 ...

  7. net user命令详解

    net use \\ip\ipc$ " " /user:" " 建立IPC空链接 net use \\ip\ipc$ "密码" /user: ...

  8. RecyclerView分割线——万能分割线

    参照网络上众多的分割线设计方法,对方法进行调整和修改,最终完成的比较通用的RecyclerView分割线,底部会附上参考网址,大家可以去看一下. 在正文之前,先说一下个人看法:研究下来,我发现,其实最 ...

  9. Model中设置表单验证方法

    Model类里面定义$_validate属性支持的验证因子格式: 格式:array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间). 验证条件: (1)Model::EXISTS_TO_ ...

  10. vue.js官方文档 PDF

    链接:https://pan.baidu.com/s/1jHMBb5W 密码:gsks