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学习day17-常用的一些模块
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- pptp,l2tp获取登录用户信息用pppd参数即可
这个问题困扰了我很久,终于在pppd的man文档里,发现了踪迹.在man中的SCRIPTS下有一系列的参数,其中PEERNAME就是登陆的用户名,并且在/etc/ppp/ip-up和/etc/ppp/ ...
- Odoo Documentation : Fields
Fields Basic fields class openerp.fields.Field(string=None, **kwargs) The field descriptor contains ...
- windows下bat批量处理启动exe
新建文本文档,start.dat start "" "D:\QQ\anzhaung\Bin\QQ.exe" 启动QQ cd ./当前文件夹下,../上一文件夹下 ...
- 使用git命令从github上clone项目
首先创建本地仓库(实际上就是创建一个文件夹,放项目代码),然后cd进文件夹, 初始化空的git仓库 注意:这里不初始化也是可以clone的 然后git clone url(url表示项目网址) 然后就 ...
- LUOGU P3157 [CQOI2011]动态逆序对(CDQ 分治)
传送门 解题思路 cdq分治,将位置看做一维,修改时间看做一维,权值看做一维,然后就转化成了三维偏序,用排序+cdq+树状数组.注意算删除贡献时要做两次cdq,分别算对前面和后面的贡献. #inclu ...
- PHP--通用化API接口数据输出 封装
/** * 通用化API接口数据输出 * author qinpeizhou * @param $message * @param array $data * @param int $httpCode ...
- 19-10-15-Night-E
信心赛??高考赛…… 过程 T1码了暴力+随机化. T2没码完.$Kuku$了 T3写了暴力+ puts("86400\n-1"); 骗了点分. T1 ××你告诉我CF E题是T1 ...
- PHP实现微信退款的分析与源码实现
原文:https://blog.csdn.net/jason19905/article/details/78628349 网上的很多PHP微信支付接入教程都颇为复杂,且需要配置和引入较多的文件,本人通 ...
- JS控制视频的播放
在用js定时器控制视频时, html部分: <video id="video_1"> <source id="source_1" src=&q ...
