https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3914

对L研究可以发现

相当于黑-横白,黑-纵白,每个黑白都要被匹配到,其中黑的横纵各两次,

很自然的想到拆点,黑点拆成专门和横白连接的,专门和纵白连接的,

如果2*黑==白数,运行一次二分匹配算法,匹配数==白数则为可行,剩下的都不可行

#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int dx[4]={0,0,1,-1};
const int dy[4]={1,-1,0,0};
char maz[600][601];
int ind[600][600];
int n,m;
bool inmaz(int x,int y){return 0<=x&&x<n&&0<=y&&y<m;}
struct pnt{
int x,y;
pnt(){x=y=0;}
pnt(int x,int y){this->x=x;this->y=y;}
};
pnt pw[600*600],pb[600*600];
int lenw,lenb;
int e[2*600*600][8];
int len[2*600*600]; int mch[2*600*600];
bool used[2*600*600]; bool dfs(int s){
used[s]=true;
for(int i=0;i<len[s];i++){
int t=e[s][i];
if(mch[t]==-1||(!used[mch[t]]&&dfs(mch[t]))){
mch[t]=s;
mch[s]=t;
return true;
}
}
return false;
} int match(){
memset(mch,-1,sizeof(int)*2*lenw);
int res=0;
for(int i=0;i<lenw*2;i++){
if(mch[i]==-1){
memset(used,false,2*lenw);
if(dfs(i)){
res++;
}
}
}
return res;
} int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
lenb=lenw=0;
memset(len,0,sizeof(len));
memset(ind,0,sizeof(ind));
for(int i=0;i<n;i++)scanf("%s",maz[i]);
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(maz[i][j]=='W'){
ind[i][j]=lenw;
pw[lenw++]=pnt(i,j);
}
else if(maz[i][j]=='B'){
ind[i][j]=lenb;
pb[lenb++]=pnt(i,j);
}
}
}
if(lenw!=lenb*2){
puts("NO");
continue;
}
for(int i=0;i<lenb;i++){
int x=pb[i].x,y=pb[i].y;
for(int j=0;j<2;j++){
int txj=x+dx[j],tyj=y+dy[j];
if(!inmaz(txj,tyj)||maz[txj][tyj]!='W')continue;
int indj=ind[txj][tyj],hor=lenw+2*i+1; e[indj][len[indj]++]=hor;
e[hor][len[hor]++]=indj;
}
for(int k=2;k<4;k++){
int txk=x+dx[k],tyk=y+dy[k];
if(!inmaz(txk,tyk)||maz[txk][tyk]!='W')continue;
int indk=ind[txk][tyk],gra=lenw+2*i;
e[indk][len[indk]++]=gra;
e[gra][len[gra]++]=indk;
}
}
int res=match();
if(res!=lenw)puts("NO");
else puts("YES");
}
return 0;
}

  

UVALive 5903 Piece it together 二分匹配,拆点 难度:1的更多相关文章

  1. UVALive 5903 Piece it together(二分图匹配)

    给你一个n*m的矩阵,每个点为'B'或'W'或'.'.然后你有一种碎片.碎片可以旋转,问可否用这种碎片精确覆盖矩阵.N,M<=500 WB  <==碎片 W 题目一看,感觉是精确覆盖(最近 ...

  2. UVALive - 7427 the math 【二分匹配】

    题目链接 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  3. UVALive 5903 Piece it together

    一开始用的STL一直超时不能过,后来发现AC的代码基本都用的普通邻接表,然后改了一下13s,T=T,效率太低了.然后把某大神,详情戳链接http://acm.hust.edu.cn/vjudge/pr ...

  4. ZOJ 3646 Matrix Transformer 二分匹配,思路,经典 难度:2

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4836 因为要使对角线所有元素都是U,所以需要保证每行都有一个不同的列上有U,设 ...

  5. UVALive 6525 Attacking rooks 二分匹配 经典题

    题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=4536">点击打开链接 题意: ...

  6. kuangbin带你飞 匹配问题 二分匹配 + 二分图多重匹配 + 二分图最大权匹配 + 一般图匹配带花树

    二分匹配:二分图的一些性质 二分图又称作二部图,是图论中的一种特殊模型. 设G=(V,E)是一个无向图,如果顶点V可分割为两个互不相交的子集(A,B),并且图中的每条边(i,j)所关联的两个顶点i和j ...

  7. HDU-4619 Warm up 2 二分匹配

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4619 一看就知道是二分匹配题目,对每个点拆点建立二分图,最后答案除2.因为这里是稀疏图,用邻接表处理. ...

  8. ZOJ 1654 二分匹配基础题

    题意: 给你一副图, 有草地(*),空地(o)和墙(#),空地上可以放机器人, 机器人向上下左右4个方向开枪(枪不能穿墙),问你在所有机器人都不相互攻击的情况下能放的最多的机器人数. 思路:这是一类经 ...

  9. HDU 3036 Escape 网格图多人逃生 网络流||二分匹配 建图技巧

    题意: 每一个' . '有一个姑娘, E是出口,'.'是空地 , 'X' 是墙. 每秒钟每一个姑娘能够走一步(上下左右) 每秒钟每一个出口仅仅能出去一个人 给定n*m的地图, 时限T 问全部姑娘是否能 ...

随机推荐

  1. html中使用JS实现图片轮播效果

    1.首先是效果图,要在网页中实现下图的轮播效果,有四张图片,每张图片有自己的标题,然后还有右下角的小方框,鼠标悬浮在小方框上,会切换到对应的图片中去. 2.先是HTML中的内容,最外层是轮播图整个的容 ...

  2. Shape of passed values is (3490, 21), indices imply (3469, 21)

    背景 处理DataFrame数据时,抛了这个错误:Shape of passed values is (3490, 21), indices imply (3469, 21) 解决 数据出现重复,导致 ...

  3. Python总结篇——知识大全

    python基础 Python开发环境搭建 Python变量和基本数据类型 python基本数据类型之操作 python的语法规范及for和while python编码 python文件操作 pyth ...

  4. hdu 5056 Boring count (窗体滑动)

    You are given a string S consisting of lowercase letters, and your task is counting the number of su ...

  5. 前端 html head meta

    META(Metadata information) 提供有页面的元信息 例如:页面编码.刷新.跳转.针对搜索引擎和更新频道的描述和关键词 1.另外一种编码写法 <meta http-equiv ...

  6. 代码处理 iOS 的横竖屏旋转

    一.监听屏幕旋转方向 在处理iOS横竖屏时,经常会和UIDeviceOrientation.UIInterfaceOrientation和UIInterfaceOrientationMask这三个枚举 ...

  7. centos linux系统日常管理复习 CPU物理数逻辑核数,iftop ,iotop ,sar ,ps,netstat ,一网卡多IP,mii-tool 连接,ethtool速率,一个网卡配置多个IP,mii-tool 连接,ethtool速率 ,crontab备份, 第十八节课

    centos linux系统日常管理复习 物理CPU和每颗CPU的逻辑核数,uptime ,w,vmstat,iftop ,iotop ,sar ,ps,netstat ,一个网卡配置多个IP,mii ...

  8. centos vim编辑器 第八节课

    centos  vim编辑器  第八节课 其他编辑器: nanogeditemacs 腾讯云上的vim版本~ VIM - Vi IMproved ~ ~ version 7.4.629 ~ by Br ...

  9. Spring boot 集成ckeditor

    1:下载ckeditor  4.4.2 full package ,官网没有显示, 需要在最新版本的ckeditor download右键,复制链接, 输入到导航栏,将版本号改为自己想要的版本号. h ...

  10. (转) 密码学中的“盐值 Salt”

    为什么要在密码里加点“盐” 盐(Salt) 在密码学中,是指通过在密码任意固定位置插入特定的字符串,让散列后的结果和使用原始密码的散列结果不相符,这种过程称之为“加盐”. 以上这句话是维基百科上对于 ...