[poj2446]Chessboard
Description
给定一个m×n的棋盘,上面有k个洞,求是否能在不重复覆盖且不覆盖到洞的情况下,用2×1的卡片完全覆盖棋盘。
Input
第一行有三个整数n,m,k(0<m,n<=32, 0<=k<m×n),m表示行数,n表示列数。
接下来k行,每行两个整数y,x,表示(x,y)上有个洞。
Output
如果能覆盖,输出YES;否则输出NO。
Sample Input
4 3 2
2 1
3 3
Sample Output
YES
Solution
32×32的范围如果暴搜剪枝能力比较强的人也许能过吧,但是我并没有那个能力。
2×1的卡片去覆盖的话,就是两个相邻的格子为一对,且每个格子只能属于一对(有洞的格子不属于任何一对)。
怎么有点像二分图匹配?那就想想怎么构图吧。
定义两格子相邻当且仅当它们有一条公共边时。
那么两个相邻的格子就得在不同集合里,按这样分正好能分成两个集合。
然后两个可放卡片的相邻格子连一条边,这样二分图就构成功了。
#include<set>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define D 5
#define N 35
#define K 513
#define M 2049
using namespace std;
struct graph{
int nxt,to;
}e[M];
int x[D]={,,,,-},y[D]={,,-,,};
int g[K],fr[N*N],m,n,k,cnt,tot,sum;
bool b[N][N],u[N*N];
inline void addedge(int x,int y){
e[++cnt].nxt=g[x];g[x]=cnt;e[cnt].to=y;
}
inline bool match(int k){
for(int i=g[k];i;i=e[i].nxt)
if(!u[e[i].to]){
u[e[i].to]=true;
if(!fr[e[i].to]||match(fr[e[i].to])){
fr[e[i].to]=k;return true;
}
}
return false;
}
inline bool hungary(){
for(int i=;i<=tot;i++){
memset(u,,sizeof(u));
if(!match(i)) return false;
}
return true;
}
inline void init(){
scanf("%d%d%d",&m,&n,&k);
for(int i=;i<=m;i++)
for(int j=;j<=n;j++)
b[i][j]=true;
for(int i=,j,l;i<=k;i++){
scanf("%d%d",&j,&l);
b[l][j]=false;
}
for(int i=;i<=m;i++)
for(int j=!(i&)+;j<=n;j+=)
if(b[i][j]){
++tot;
for(int l=;l<D;l++)
if(b[i+x[l]][j+y[l]])
addedge(tot,(i+x[l]-)*n+j+y[l]);
}
for(int i=;i<=m;i++)
for(int j=(i&)+;j<=n;j+=)
if(b[i][j]) sum++;
if(sum==tot&&hungary()) printf("YES\n");
else printf("NO\n");
}
int main(){
freopen("chessboard.in","r",stdin);
freopen("chessboard.out","w",stdout);
init();
fclose(stdin);
fclose(stdout);
return ;
}
[poj2446]Chessboard的更多相关文章
- [POJ2446] Chessboard(二分图最大匹配-匈牙利算法)
传送门 把所有非障碍的相邻格子彼此连一条边,然后求二分图最大匹配,看 tot * 2 + k 是否等于 n * m 即可. 但是连边不能重复,比如 a 格子 和 b 格子 相邻,不能 a 连 b ,b ...
- poj2446 Chessboard 【最大匹配】
题目大意:一个n*m的棋盘,某些格子不能用,问用1*2的骨牌能否完全覆盖这个棋盘,当然,骨牌不能有重叠 思路:显然黑白染色后,一个骨牌只能覆盖一个白色格子和一个黑色格子,然后我们间二染色建图,看能否有 ...
- LightOJ1171 Knights in Chessboard (II)(二分图最大点独立集)
题目 Source http://www.lightoj.com/volume_showproblem.php?problem=1171 Description Given an m x n ches ...
- CodeForces445A DZY Loves Chessboard
A. DZY Loves Chessboard time limit per test 1 second memory limit per test 256 megabytes input stand ...
- FJOI省队集训 chessboard
(题目懒得打字了,建议到新窗口查看) 显然这玩意儿是可以按位搞的...然后就是一个裸的最小割模型? 然而这样做理论上只有30分实际上有40分. 事实上我们可以发现,每一列的取值只和上一列有关,这样我们 ...
- UESTC 1851 Kings on a Chessboard
状压DP... Kings on a Chessboard Time Limit: 10000ms Memory Limit: 65535KB This problem will be judged ...
- UVa12633 Super Rooks on Chessboard(容斥 + FFT)
题目 Source http://acm.hust.edu.cn/vjudge/problem/42145 Description Let’s assume there is a new chess ...
- poj 2446 Chessboard (二分匹配)
Chessboard Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12800 Accepted: 4000 Descr ...
- 周赛-DZY Loves Chessboard 分类: 比赛 搜索 2015-08-08 15:48 4人阅读 评论(0) 收藏
DZY Loves Chessboard time limit per test 1 second memory limit per test 256 megabytes input standard ...
随机推荐
- Python __init__.py 作用详解
__init__.py 文件的作用是将文件夹变为一个Python模块,Python 中的每个模块的包中,都有__init__.py 文件. 通常__init__.py 文件为空,但是我们还可以为它增加 ...
- HTML5+JS 《五子飞》游戏实现(五)移动棋子
上一章 我们知道了怎么处理两个重要的吃棋动作,想要吃对方的棋子,首先得移动自己的棋子.现在里沃特跟大家分享分享,怎么移动棋子. 想要移动棋子,在页面上,首先要点击一下要移动的棋子,然后再点击一下目标位 ...
- 新时代的coder如何成为专业程序员
在移动互联网"泛滥"的今天,越来越多非专业(这里的非专业指的是非计算机专业毕业的程序员)程序员加入到了IT行业中来了,可能是因为移动互联网的火爆导致程序员容易就业而且工资很高,可能 ...
- position为absolute的元素如何实现居中
当给div设置absolute时,该元素已经脱离文档流,呈现浮动状态,只能通过left,top,right,bottom来设置属性,要实现居中有两种方法: 一.css法 <div class=& ...
- 前端 head 中mate 详解
<meta name="viewport" content="width=device-width,height=device-height,initial-sca ...
- C程序两则
<span style="font-size:24px;">#include<iostream> using namespace std; int *fun ...
- webpack 插件: html-webpack-plugin
插件地址:https://www.npmjs.com/package/html-webpack-plugin 这个插件用来简化创建服务于 webpack bundle 的 HTML 文件,尤其是对于在 ...
- C++11异常处理 noexcept
1.简介 在C语言中,如果程序的运行出现异常.错误,我们想提供方案处理这些异常时,我们面临许多问题,如: (1)C语言没有提供统一(标准)的方式来处理错误: (2)无法保证错误会被正确的处理: (3) ...
- eclipse的插件安装
如何安装:http://my.oschina.net/linjunlong/blog/126961 插件安装方法:eclipse和myeclipse版本不一样,略有区别 在线安装: 第一种:知道在线安 ...
- CSS3动画属性之Animation
首先定义一个动画规则: @keyframes mymove { from {top:0px;} to {top:200px;} } @-moz-keyframes mymove /* Firefox ...