HDU1045(KB10-A 二分图最大匹配)
2017-08-21 11:31
246 查看
Fire Net
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12575 Accepted Submission(s): 7614
Problem Description
Suppose 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.
Input
The 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.
Output
For each test case, output one line containing the maximum number of blockhouses that can be placed in the city in a legal configuration.Sample Input
4 .X.. .... XX.. .... 2 XX .X 3 .X. X.X .X. 3 ... .XX .XX 4 .... .... .... .... 0Sample Output
5 1 5 2 4
Source
Zhejiang University Local Contest 2001 X集合表示棋盘一行被墙分隔的点,Y表示棋盘一列被墙分隔的点,若(i,j)位置有棋子,则i在X集合中对应的点与j在Y集合中对应的点之间有一条连线,问题转换为二分图最大匹配。//2017-08-21 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int N = 50; int head , tot; struct Edge{ int to, next; }edge[N<<2]; void init(){ tot = 0; memset(head, -1, sizeof(head)); } void add_edge(int u, int v){ edge[tot].to = v; edge[tot].next = head[u]; head[u] = tot++; edge[tot].to = u; edge[tot].next = head[v]; head[v] = tot++; } int n; string G ; int matching ; int check ; bool dfs(int u){ for(int i = head[u]; i != -1; i = edge[i].next){ int v = edge[i].to; if(!check[v]){//要求不在交替路 check[v] = 1;//放入交替路 if(matching[v] == -1 || dfs(matching[v])){ //如果是未匹配点,说明交替路为增广路,则交换路径,并返回成功 matching[u] = v; matching[v] = u; return true; } } } return false;//不存在增广路 } //hungarian: 二分图最大匹配匈牙利算法 //input: null //output: ans 最大匹配数 int hungarian(){ int ans = 0; memset(matching, -1, sizeof(matching)); for(int u = 0; u < n*n; u++){ if(matching[u] == -1){ memset(check, 0, sizeof(check)); if(dfs(u)) ans++; } } return ans; } int main() { //freopen("inputA.txt", "r", stdin); while(cin>>n && n){ init(); for(int i = 0; i < n; i++) cin>>G[i]; int id = 0, row_id[5][5], col_id[5][5]; memset(row_id, -1, sizeof(row_id)); memset(col_id, -1, sizeof(col_id)); for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ if(G[i][j] == '.')row_id[i][j] = id; else if(G[i][j] == 'X')id++; } id++; } id = n*n; for(int j = 0; j < n; j++){ for(int i = 0; i < n; i++){ if(G[i][j] == '.')col_id[i][j] = id; else if(G[i][j] == 'X')id++; } id++; } for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) if(row_id[i][j] != -1 && col_id[i][j] != -1) add_edge(row_id[i][j], col_id[i][j]); cout<<hungarian()<<endl; } return 0; }
相关文章推荐
- HDU 2444 The Accomodation of Students 二分图判定+最大匹配
- HDU:2063 过山车(二分图最大匹配)
- 二分图判断(+最大匹配)NYOJ 1015+239 HDU 2444
- [ACM] HDU 3395 Special Fish (二分图最大权匹配,KM算法)
- [ACM] HDU 2255 奔小康赚大钱 (二分图最大权匹配,KM算法)
- hdu 1281 二分图最大匹配
- hdu 2444 The Accomodation of Students(二分图判断,二分图最大匹配)
- HDU 1528 Card Game Cheater(匈牙利算法,二分图最大匹配):
- Oil Skimming HDU - 4185 (二分图最大匹配)
- HDU 2444 黑白染色判二分图+二分最大匹配
- hdu 1150 二分图的最大匹配 hopcroft-karp算法
- hdu 3861 The King’s Problem (tarjan 算法+二分图最大匹配)
- hdu 1528 Card Game Cheater(二分图的最大匹配)
- hdu 2819 Swap 二分图的最大匹配
- hdu 1281 二分图最大匹配
- HDU 2063 过山车(匈牙利算法)(二分图求最大匹配)
- hdu 4619 Warm up 2 ( 二分图最大匹配 )
- hdu 3722 Card Game 二分图的最大权匹配
- HDU 2063 过山车(模板—— 二分图最大匹配问题)
- HDU 3395Special Fish(二分图的最大权匹配)