题意:给定一个 n * n的图,X是卒, . 是空位置,让你放尽量多的车,使得他们不互相攻击。

析:把每行连续的 . 看成X集体的一个点,同理也是这样,然后求一个最大匹配即可。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
//#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 100 + 10;
const int maxm = 3e5 + 10;
const ULL mod = 3;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} char s[maxn][maxn];
int a[maxn][maxn], b[maxn][maxn]; struct Edge{
int to, next;
};
Edge edges[maxn*maxn];
int head[maxn*maxn], cnt; void addEdge(int u, int v){
edges[cnt].to = v;
edges[cnt].next = head[u];
head[u] = cnt++;
} int match[maxn*maxn];
bool used[maxn*maxn]; bool dfs(int u){
used[u] = 1;
for(int i = head[u]; ~i; i = edges[i].next){
int v = edges[i].to, w = match[v];
if(w == -1 || !used[w] && dfs(w)){
match[u] = v;
match[v] = u;
return true;
}
}
return false;
} int main(){
while(scanf("%d", &n) == 1){
int idx = 0;
for(int i = 0; i < n; ++i){
scanf("%s", s[i]);
if(s[i][0] == '.') a[i][0] = ++idx;
for(int j = 1; j < n; ++j)
if(s[i][j] == '.' && s[i][j] == s[i][j-1]) a[i][j] = a[i][j-1];
else if(s[i][j] == '.') a[i][j] = ++idx;
}
int row = idx;
for(int j = 0; j < n; ++j){
if(s[0][j] == '.') b[0][j] = ++idx;
for(int i = 1; i < n; ++i)
if(s[i][j] == '.' && s[i][j] == s[i-1][j]) b[i][j] = b[i-1][j];
else if(s[i][j] == '.') b[i][j] = ++idx;
}
cnt = 0; ms(head, -1);
FOR(i, 0, n) FOR(j, 0, n) if(s[i][j] == '.'){
addEdge(a[i][j], b[i][j]);
addEdge(b[i][j], a[i][j]);
}
int ans = 0; ms(match, -1);
for(int i = 1; i <= row; ++i) if(match[i] < 0){
ms(used, 0); if(dfs(i)) ++ans;
}
printf("%d\n", ans);
}
return 0;
}

  

UVaLive 6525 Attacking rooks (二分图最大匹配)的更多相关文章

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

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

  2. [ An Ac a Day ^_^ ] UVALive 2635 Housing Complexes 二分图最大匹配

    快要比赛了 看看原来做过的题 感觉这道题当时做的还是挺费劲的 所以发一下 题意: 一个土豪要建别墅 因为有的地区地方不够大 所以要拆屋子 每个地方的字母就是对应开发商的地盘 没有字母的就是自由土地 一 ...

  3. UVALive 5033 I'm Telling the Truth 二分图最大匹配(略有修改)

    I - I'm Telling the Truth Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu ...

  4. HDU 5093 Battle ships(二分图最大匹配)

    题意:一个m行n列的图由#.*.o三种符号组成,分别代表冰山.海域.浮冰,问最多可放的炮舰数(要求满足以下条件) 1.炮舰只可放在海域处 2.两个炮舰不能放在同一行或同一列(除非中间隔着一个或多个冰山 ...

  5. POJ 2226二分图最大匹配

    匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名.匈牙利算法是基于Hall定理中充分性证明的思想,它是二部图匹配最常见的算法,该算法的核心就是寻找增广路径,它是一种用增广路径求二分图 ...

  6. POJ2239 Selecting Courses(二分图最大匹配)

    题目链接 N节课,每节课在一个星期中的某一节,求最多能选几节课 好吧,想了半天没想出来,最后看了题解是二分图最大匹配,好弱 建图: 每节课 与 时间有一条边 #include <iostream ...

  7. poj 2239 二分图最大匹配,基础题

    1.poj 2239   Selecting Courses   二分图最大匹配问题 2.总结:看到一个题解,直接用三维数组做的,很巧妙,很暴力.. 题意:N种课,给出时间,每种课在星期几的第几节课上 ...

  8. UESTC 919 SOUND OF DESTINY --二分图最大匹配+匈牙利算法

    二分图最大匹配的匈牙利算法模板题. 由题目易知,需求二分图的最大匹配数,采取匈牙利算法,并采用邻接表来存储边,用邻接矩阵会超时,因为邻接表复杂度O(nm),而邻接矩阵最坏情况下复杂度可达O(n^3). ...

  9. 二分图最大匹配的K&#246;nig定理及其证明

     二分图最大匹配的K?nig定理及其证明 本文将是这一系列里最短的一篇,因为我只打算把K?nig定理证了,其它的废话一概没有.    以下五个问题我可能会在以后的文章里说,如果你现在很想知道的话,网上 ...

随机推荐

  1. UI5-文档-4.33-Routing Back and History

    现在我们可以导航到细节页面并显示发票,但是还不能回到概览页面.我们将向细节页面添加一个back按钮,并实现一个函数,再次显示概述页面. Preview A back button is now dis ...

  2. 通过yumdownloader下载rpm包

    通过yum自带的一个工具:yumdownloader [root@web1 ~]#  rpm -qa |grep yum-utils [root@web1 ~]# yum -y install yum ...

  3. 关于HTTP协议及SOCKET通信

    一.HTTP 1.报文结构 HTTP:超文本传输协议,报文分为请求报文和响应报文. 2.端口(tomcat端口) http在熟知的80端口使用TCP的服务:tomcat的默认端口是8080 3.状态码 ...

  4. python-淘宝信息定向爬取

    S是类似产品页数  bcoffset直流偏移. 有人在将偏移量:http://www.cnblogs.com/defineconst/p/6185396.html item.taobao.com/it ...

  5. sqlserver解密加密的存储过程(图解)

    来自博客:http://www.cnblogs.com/wghao/archive/2012/12/30/2837642.html

  6. 如何写一个自定义的js文件

    自定义一个Utils.js文件,在其中写js代码即可.如: (function(w){ function Utils(){} Utils.prototype.getChilds = function( ...

  7. Ubuntu 安装 Zabbix 3.2详细步骤

    创建 zabbix 用户 因为zabbix 程序的守护进程需要非特权用户,所以需要创建一个 zabbix 用户,即使从 root 用户启动 zabbix 程序,也会自动切换到 zabbix 用户,所以 ...

  8. 'org.hibernate.SQLQuery' is deprecated

    'org.hibernate.SQLQuery' is deprecated 在Hibernate5.2之后,SQLQuery已经被摒弃,改用NativeQuery代替了. 在Hibernate中使用 ...

  9. 87. Scramble String (String; DP)

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  10. CloseableHttpClient(二)

    package com.cmy.httpClient; import java.io.IOException; import org.apache.http.HttpEntity; import or ...