Fellow me on GitHub

HDU1045(KB10-A 二分图最大匹配)

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 .... .... .... .... 0
 

 

Sample Output

5 1 5 2 4
 

 

Source

 
X集合表示棋盘一行被墙分隔的点,Y表示棋盘一列被墙分隔的点,若(i,j)位置有棋子,则i在X集合中对应的点与j在Y集合中对应的点之间有一条连线,问题转换为二分图最大匹配。
 
  1 //2017-08-21
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <iostream>
  5 #include <algorithm>
  6 
  7 using namespace std;
  8 
  9 const int N = 50;
 10 int head[N], tot;
 11 struct Edge{
 12     int to, next;
 13 }edge[N<<2];
 14 
 15 void init(){
 16     tot = 0;
 17     memset(head, -1, sizeof(head));
 18 }
 19 
 20 void add_edge(int u, int v){
 21     edge[tot].to = v;
 22     edge[tot].next = head[u];
 23     head[u] = tot++;
 24 
 25     edge[tot].to = u;
 26     edge[tot].next = head[v];
 27     head[v] = tot++;
 28 }
 29 
 30 int n;
 31 string G[N];
 32 int matching[N];
 33 int check[N];
 34 
 35 bool dfs(int u){
 36     for(int i =  head[u]; i != -1; i = edge[i].next){
 37         int v = edge[i].to;
 38         if(!check[v]){//要求不在交替路
 39             check[v] = 1;//放入交替路
 40             if(matching[v] == -1 || dfs(matching[v])){
 41                 //如果是未匹配点,说明交替路为增广路,则交换路径,并返回成功
 42                 matching[u] = v;
 43                 matching[v] = u;
 44                 return true;
 45             }
 46         }
 47     }
 48     return false;//不存在增广路
 49 }
 50 
 51 //hungarian: 二分图最大匹配匈牙利算法
 52 //input: null
 53 //output: ans 最大匹配数
 54 int hungarian(){
 55     int ans = 0;
 56     memset(matching, -1, sizeof(matching));
 57     for(int u = 0; u < n*n; u++){
 58         if(matching[u] == -1){
 59             memset(check, 0, sizeof(check));
 60             if(dfs(u))
 61                   ans++;
 62         }
 63     }
 64     return ans;
 65 }
 66 
 67 int main()
 68 {
 69     //freopen("inputA.txt", "r", stdin);
 70     while(cin>>n && n){
 71         init();
 72         for(int i = 0; i < n; i++)
 73               cin>>G[i];
 74         int id = 0, row_id[5][5], col_id[5][5];
 75         memset(row_id, -1, sizeof(row_id));
 76         memset(col_id, -1, sizeof(col_id));
 77         for(int i = 0; i < n; i++){
 78             for(int j = 0; j < n; j++){
 79                 if(G[i][j] == '.')row_id[i][j] = id;
 80                 else if(G[i][j] == 'X')id++;
 81             }
 82             id++;
 83         }
 84         id = n*n;
 85         for(int j = 0; j < n; j++){
 86             for(int i = 0; i < n; i++){
 87                 if(G[i][j] == '.')col_id[i][j] = id;
 88                 else if(G[i][j] == 'X')id++;
 89             }
 90             id++;
 91         }
 92         for(int i = 0; i < n; i++)
 93               for(int j = 0; j < n; j++)
 94                   if(row_id[i][j] != -1 && col_id[i][j] != -1)
 95                       add_edge(row_id[i][j], col_id[i][j]);
 96         cout<<hungarian()<<endl;
 97     }
 98 
 99     return 0;
100 }

 

 

posted @ 2017-08-21 11:31  Penn000  阅读(194)  评论(0编辑  收藏  举报