解题思路:二分图的最大匹配,但这题是所有点都遍历一遍,所以答案/2;

代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#define maxn 100500
using namespace std;
struct Edge
{
int next;
int to;
int fa;
int w;
}edge[maxn];
char s[60][60];
int t,n;
int cnt;
int visit[maxn];
int head[maxn];
int match[maxn];
void add(int u,int v)
{
edge[cnt].next=head[u];
edge[cnt].to=v;
head[u]=cnt++;
}
bool dfs(int u)
{
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(!visit[v])
{
visit[v]=1;
if(match[v]==-1||dfs(match[v]))
{
match[v]=u;return 1;
} }
}
return 0;
}
int main()
{
int tt=0;
cin>>t;
while(t--)
{
tt++;
memset(head,-1,sizeof(head));
cnt=0;
cin>>n;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
cin>>s[i][j];
for(int i=1;i<=n;i++)
{
for(int j=1;j<n;j++)
{
if(s[i][j]=='#'&&s[i][j+1]=='#')
{
// cout<<j+(i-1)*n<<" "<<j+(i-1)*n+1<<endl;
add(j+(i-1)*n,j+(i-1)*n+1);
add(j+(i-1)*n+1,j+(i-1)*n);
}
}
}
for(int j=1;j<=n;j++)
{
for(int i=1;i<n;i++)
{
if(s[i][j]=='#'&&s[i+1][j]=='#')
{
// cout<<j+(i-1)*n<<" "<<j+(i-1)*n+n<<endl;
add(j+(i-1)*n,j+(i-1)*n+n);
add(j+(i-1)*n+n,j+(i-1)*n);
}
}
}
int ans=0;
memset(match,-1,sizeof(match));
for(int i=1;i<=n*n;i++)
{
memset(visit,0,sizeof(visit));
if(dfs(i))
ans++;
}
cout<<"Case "<<tt<<": "<<ans/2<<endl;
}
}

  

最新文章

  1. unix shell命令
  2. JDK Environment Variable And Change default JDK
  3. PHP与MySQL中编码的设置
  4. Aspose 导出excel小demo
  5. SpringBoot入门 一 构建简单工程
  6. RH253读书笔记(2)-Lab 2 System Resource Access Controls
  7. javascript继承—继承的实现原理(1)
  8. python bz2模块
  9. 从锅炉工到AI专家(3)
  10. WebForm服务器验证控件与前端js自定义验证共同使用
  11. vue中使用video插件vue-video-player
  12. 理论篇-MySQL知识汇总
  13. python http请求及多线程应用
  14. Alpha冲刺-(9/10)
  15. MySQL基本简单操作02
  16. 对Maven项目进行强制更新
  17. cocos游戏网址
  18. 数组的翻转(非reverse())
  19. 数字的可视化:python画图之散点图sactter函数详解
  20. Leetcode[1]Two Sum C++

热门文章

  1. selenium:断言
  2. python文件流
  3. Vue2.x源码学习笔记-Vue构造函数
  4. abp 基于api接口的页面内容提交
  5. 深入浅出Tomcat/3 - Tomcat生命周期
  6. Windows Community Toolkit 4.0 - DataGrid - Part03
  7. LeetCode 832. Flipping an Image
  8. vuex mapState、mapGetters、mapActions、mapMutations的使用
  9. 二十一、当锚点遇到fixed(margin和padding)
  10. session与cookie的区别以及HTML5中WebStorage理解