Hdu 1045 二分匹配
Fire Net
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6282 Accepted Submission(s): 3551Problem DescriptionSuppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall.A blockhouse is a small castle that has four openings through which to shoot. The four openings are facing North, East, South, and West, respectively. There will be one machine gun shooting through each opening.
Here we assume that a bullet is so powerful that it can run across any distance and destroy a blockhouse on its way. On the other hand, a wall is so strongly built that can stop the bullets.
The goal is to place as many blockhouses in a city as possible so that no two can destroy each other. A configuration of blockhouses is legal provided that no two blockhouses are on the same horizontal row or vertical column in a map unless there is at least one wall separating them. In this problem we will consider small square cities (at most 4x4) that contain walls through which bullets cannot run through.
The following image shows five pictures of the same board. The first picture is the empty board, the second and third pictures show legal configurations, and the fourth and fifth pictures show illegal configurations. For this board, the maximum number of blockhouses in a legal configuration is 5; the second picture shows one way to do it, but there are several other ways.
Your task is to write a program that, given a description of a map, calculates the maximum number of blockhouses that can be placed in the city in a legal configuration.
InputThe input file contains one or more map descriptions, followed by a line containing the number 0 that signals the end of the file. Each map description begins with a line containing a positive integer n that is the size of the city; n will be at most 4. The next n lines each describe one row of the map, with a '.' indicating an open space and an uppercase 'X' indicating a wall. There are no spaces in the input file.OutputFor each test case, output one line containing the maximum number of blockhouses that can be placed in the city in a legal configuration.Sample Input4
.X..
....
XX..
....
2
XX
.X
3
.X.
X.X
.X.
3
...
.XX
.XX
4
....
....
....
....
0Sample Output5
1
5
2
4
/*************************************************************************
> File Name: 1045.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年08月01日 星期五 23时01分49秒
> Propose:
************************************************************************/
#include <queue>
#include <cmath>
#include <string>
#include <cstdio>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; char map[][];
int cx[], cy[];
int row[][], col[][];
bool mark[];
int G[][];
int n, cnt1, cnt2; int path(int u) {
for (int i = ; i < cnt2; i++) {
if (G[u][i] && !mark[i]) {
mark[i] = true;
if (cy[i] == - || path(cy[i])) {
cy[i] = u;
cx[u] = i;
return ;
}
}
}
return ;
} int MaxMatch() {
memset(cx, -, sizeof(cx));
memset(cy, -, sizeof(cy));
int res = ;
for (int i = ; i < cnt1; i++) {
if (cx[i] == -) {
memset(mark, false, sizeof(mark));
res += path(i);
}
}
return res;
} int main(void) {
while (~scanf("%d", &n) && n) {
for (int i = ; i < n; i++) {
scanf("%s", map[i]);
}
cnt1 = ;
for (int i = ; i < n; i++) {
for (int j = ; j < n; j++) {
col[i][j] = cnt1;
if (j == n- || map[i][j] == 'X') cnt1++;
}
}
cnt2 = ;
for (int i = ; i < n; i++) {
for (int j = ; j < n; j++) {
row[j][i] = cnt2;
if (j == n- || map[j][i] == 'X') cnt2++;
}
}
memset(G, , sizeof(G));
for (int i = ; i < n; i++)
for (int j = ; j < n; j++) if (map[i][j] != 'X') {
G[row[i][j]][col[i][j]] = ;
}
printf("%d\n", MaxMatch());
} return ;
}
Hdu 1045 二分匹配的更多相关文章
- hdu 4169 二分匹配最大独立集 ***
题意:有水平N张牌,竖直M张牌,同一方向的牌不会相交.水平的和垂直的可能会相交,求最少踢出去几张牌使剩下的牌都不相交. 二分匹配 最小点覆盖=最大匹配. 链接:点我 坐标点作为匹配的端点 #inclu ...
- hdu 5093 二分匹配
/* 题意:给你一些冰岛.公共海域和浮冰,冰岛可以隔开两个公共海域,浮冰无影响 求选尽可能多的选一些公共海域点每行每列仅能选一个. 限制条件:冰山可以隔开这个限制条件.即*#*可以选两个 预处理: * ...
- Battle ships HDU - 5093二分匹配
Battle shipsHDU - 5093 题目大意:n*m的地图,*代表海洋,#代表冰山,o代表浮冰,海洋上可以放置船舰,但是每一行每一列只能有一个船舰(类似象棋的車),除非同行或者同列的船舰中间 ...
- hdu 4685 二分匹配+强连通分量
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4685 题解: 这一题是poj 1904的加强版,poj 1904王子和公主的人数是一样多的,并且给出 ...
- H - Prince and Princess - HDU 4685(二分匹配+强连通分量)
题意:有N个王子M个公主,王子喜欢一些公主,而且只能是王子喜欢的人,他们才可以结婚,现在让他们尽可能多的结婚的前提下找出来每个王子都可以和谁结婚. 分析:先求出来他们的最大匹配,因为给的数据未必是完备 ...
- HDU 3729 二分匹配 反向匹配
题意: 给定 n个学生 说的 自己 考试排名的 可能范围 确定最多几个人说真话 如果有多种答案,输出字典序最大的那种( 要求字典序最大,所以solve中从最大字典序开始匹配) 思路: 题目给定 点 ...
- HDU -1151 二分匹配与有向无环图不相交最小路径覆盖数
题意: 考虑一个小镇,那里的所有街道都是单向的,并且每条街道都从一个路口通往另一个路口.还众所周知,从一个十字路口开始,穿过城镇的街道,您将永远无法到达同一十字路口,即,城镇的街道没有环. 基于这些假 ...
- HDU 2603 二分匹配
#include <queue>#include <vector>#include <cstdio>#include <cstring>#include ...
- hdu 1528 二分匹配
#include<stdio.h> #include<string.h> int map[100][100],mark[100],link[100],max2,k; int f ...
- Fire Net HDU - 1045 (二分图匹配)
题意: 给出一张图,图中'X'表示wall,'.'表示空地,可以放置blockhouse同一条直线上只能有一个blockhouse,除非有wall 隔开,问在给出的图中最多能放置多少个blockhou ...
随机推荐
- python编码知识初始_ASCII码,Unicode,Utf-8,GBK
谍战片,电报,摩斯密码,相应规则(暗号),编码解码: 电脑底层是高低电平来传输信息(OSI七层模型,最底层):文件存储的本质,也是二进制,01010101 美国:ASCII码(8位表示一个字节 000 ...
- C/C++基础:main函数的参数argc和argv
转载: https://blog.csdn.net/Eastmount/article/details/20413773 一.main()函数参数 通常我们在写主函数时都是void main()或in ...
- 排列组合lucas模板
//codeforces 559C|51nod1486 Gerald and Giant Chess(组合数学+逆元) #include <bits/stdc++.h> using nam ...
- Leetcode951. Flip Equivalent Binary Trees翻转等价二叉树
我们可以为二叉树 T 定义一个翻转操作,如下所示:选择任意节点,然后交换它的左子树和右子树. 只要经过一定次数的翻转操作后,能使 X 等于 Y,我们就称二叉树 X 翻转等价于二叉树 Y. 编写一个判断 ...
- cannot be cast to javax.servlet.Servlet 解决
使用maven创建web项目的时候,通过添加依赖的方式来添加servlet-api,如下 通过maven的命令(tomcat:run)来启动项目,发现访问的时候报错,错误如下: 错误排查: 首先查看s ...
- JZOJ5898【NOIP2018模拟10.6】距离统计
题目 题目大意 给你带边权的树,然后有多高询问,每次询问距离某个点第kkk近的节点的距离. 思考 一眼看下去,首先就是想到如何动态的区间第K大,还要支持区间修改-- 于是想了半天,觉得不可做-- 最终 ...
- vbox虚拟机复制&&虚拟机指定静态IP
一.复制镜像(假设源镜像已经用桥接方式,可以访问互联网). 注意需要重新生成mac地址 二.复制完成,启动复制好的镜像(注意,此时的镜像无法联网) vi /etc/udev/rules.d/70-pe ...
- 编写一个函数isMerge,判断一个字符串str是否可以由其他两个字符串part1和part2“组合”而成
编写一个函数isMerge,判断一个字符串str是否可以由其他两个字符串part1和part2“组合”而成.“组合 ”的规则如下: 1). str中的每个字母要么来自于part1,要么来自于part2 ...
- C++标准输入问题
1.读取数据量不定的输入数据 e.g. #include <iostream> using namespace std; void main() { ,val=; while(cin> ...
- Jmeter环境搭建及目录介绍
Jmeter环境搭建及目录介绍 运行环境:jmeter是基于java语言的,所有需要准备Java的JDK环境,并添加到环境变量中. 一.环境搭建及配置 1.JMeter的下载: 官网:http://j ...
