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 ...
随机推荐
- PAT甲级——A1081 Rational Sum
Given N rational numbers in the form numerator/denominator, you are supposed to calculate their sum. ...
- 03-python 学习第三天:文件操作
今天学习了打开文件.读取文件.追加内容的操作. 操作实例1:修改文件 思路: 1.修改文件一般有两种方法,一是将文件读取后加载到内存中修改然后写入磁盘,第二种方法是逐行读取并处理.小的文件用第一种方法 ...
- VC控件DateTimePicker使用方法
出自http://www.cnblogs.com/52yixin/articles/2111299.html 使用DateTimePicker控件一般是获 取其时间替代手工输入带来的不便,而DateT ...
- 打开springboot的run dashboard
默认情况下,idea的run dashboard是关闭的,当检测到你有多个springboot项目时会弹出提示框,询问是否打开. 如果我们想要自己打开,需要修改配置. 在你的idea的项目目录中,有一 ...
- Leetcode515. Find Largest Value in Each Tree Row在每个树行中找最大值
您需要在二叉树的每一行中找到最大的值. 示例: 输入: 1 / \ 3 2 / \ \ 5 3 9 输出: [1, 3, 9] class Solution { public: vector<i ...
- NOIP2016提高A组 A题 礼物—概率状压dp
题目描述 夏川的生日就要到了.作为夏川形式上的男朋友,季堂打算给夏川买一些生 日礼物. 商店里一共有n种礼物.夏川每得到一种礼物,就会获得相应喜悦值Wi(每种礼物的喜悦值不能重复获得). 每次,店员会 ...
- redis中重启和停止服务
首先需要打开两个终端,一个是服务端,一个是客户端 1.开启服务端 redis-server 2.开启客户端 redis-cli 关闭双方之间的连接: 在客户端中输入:redis-cli shutdow ...
- springmvc:请求参数绑定集合类型
一.请求参数绑定实体类 domain: private String username; private String password; private Double money; private ...
- 教你用webpack搭一个vue脚手架[超详细讲解和注释!](转载)
1.适用人群 1.对webpack知识有一定了解但不熟悉的同学. 2.女同学!!!(233333....) 2.目的 在自己对webpack有进一步了解的同时,也希望能帮到一些刚接触webpack的同 ...
- JAVA面试常见问题之Redis篇
Redis为单线程 1.Redis 有哪些数据类型 String 哈希 list set 有序set 2.Redis 内部结构 参考:https://www.cnblogs.com/chenpingz ...