[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 ...
随机推荐
- linux运维中的命令梳理(二)
回想起来,从事linux运维工作已近5年之久了,日常工作中会用到很多常规命令,之前简单罗列了一些命令:http://www.cnblogs.com/kevingrace/p/5985486.html今 ...
- HTML5+jquery整屏页面切换效果
压缩包下载 演示地址 http://www.yyyweb.com/demo/page-transitions/
- 独立成分分析(ICA)在fMRI数据处理时timecourse的理解
来源: http://blog.sciencenet.cn/blog-479412-434990.html 在处理fMRI数据时,使用空间ICA的方法.将一个四维的fMRI数据分解为空间patte ...
- C#中excel导入sql
using Microsoft.Office.Interop.Excel; public int ledinExcel(string file, object sender, EventArgs e) ...
- TinyFrame升级之四:IOC容器
在这个框架中,我们使用Autofac作为IOC容器,来实现控制反转,依赖注入的目的. 在程序加载的时候,我需要将系统中所有用到的接口与之对应的实现进行装载.由于用户交互部分是在TinyFrame.We ...
- [MetaHook] BaseUI hook
Hook IBaseUI function. #include <metahook.h> #include <IBaseUI.h> IBaseUI *g_pBaseUI = ; ...
- 阿里巴巴高新能数据源com.alibaba.druid.pool.DruidDataSource的jar包配置
aspectjweaver-1.7.4.jar druid-0.2.9.jar 两个包,用于提供com.alibaba.druid.pool.DruidDataSource
- iOS 隐藏/去掉 导航栏返回按钮中的文字
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(, -) forBarMetrics:U ...
- Yii 字段验证
关于验证的属性: $enableClientValidation:是否在客户端验证,也即是否生成前端js验证脚本(如果在form中设置了ajax验证,也会生成这个js脚本). $enableAjaxV ...
- 关于出现 org.apache.commons.lang.exception.NestableRuntimeException的解决方法
最近做服务端和客户端之间的访问,出现了 org.apache.commons.lang.exception.NestableRuntimeException等状况.实在令人头大,翻到了一个很好的帖子说 ...