HDU1045 Fire Net —— 二分图最大匹配

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1045

Fire Net

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12920    Accepted Submission(s): 7840


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
 

题解:

此题可以直接枚举,但如果数据更大的话,就需要用二分图来解了:

1.对于每一行,把连通的部分缩成一个点,并为之编号xid。同理,每一列也如此yid。

2.如果a[x][y]是空格,那么就在点xid[x][y]与点yid[x][y]之间连一条边。表明:如果在[x][y]处放置一个棋子,那么在点xid[x][y]和点yid[x][y]所涉及到的区域里,已经存在着攻击,所以不能再放其他棋子。

3.求出最大匹配数,就是x坐标与y坐标的最大组合数,每一对组合,都代表着一个可放置点(影响的范围为一段区域),所以最大匹配数,即为最大放置数。

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <string>
 6 #include <vector>
 7 #include <map>
 8 #include <set>
 9 #include <queue>
10 #include <sstream>
11 #include <algorithm>
12 using namespace std;
13 const int INF = 2e9;
14 const int MOD = 1e9+7;
15 const int MAXN = 10+10;
16 
17 int n, uN, vN;
18 char a[MAXN][MAXN];
19 int M[MAXN][MAXN], xid[MAXN][MAXN], yid[MAXN][MAXN], link[MAXN];
20 bool vis[MAXN];
21 
22 bool dfs(int u)
23 {
24     for(int i = 1; i<=vN; i++)
25     if(M[u][i] && !vis[i])
26     {
27         vis[i] = true;
28         if(link[i]==-1 || dfs(link[i]))
29         {
30             link[i] = u;
31             return true;
32         }
33     }
34     return false;
35 }
36 
37 int hungary()
38 {
39     int ret = 0;
40     memset(link, -1, sizeof(link));
41     for(int i = 1; i<=uN; i++)
42     {
43         memset(vis, 0, sizeof(vis));
44         if(dfs(i)) ret++;
45     }
46     return ret;
47 }
48 
49 
50 int main()
51 {
52     while(scanf("%d", &n) && n)
53     {
54         for(int i = 1; i<=n; i++)
55             scanf("%s", a[i]+1);
56 
57         uN = vN = 0;
58         for(int i = 1; i<=n; i++)   //每一行或列,把连通的部分缩成一点
59         for(int j = 1; j<=n; j++)
60         {
61             if(a[i][j]=='.')
62             {
63                 if(j==1 || a[i][j-1]=='X') ++uN;
64                 xid[i][j] = uN;
65             }
66 
67             if(a[j][i]=='.')
68             {
69                 if(j==1 || a[j-1][i]=='X') ++vN;
70                 yid[j][i] = vN;
71             }
72         }
73 
74         memset(M, false, sizeof(M));
75         for(int i = 1; i<=n; i++)
76         for(int j = 1; j<=n; j++)
77             if(a[i][j]!='X')
78                 M[xid[i][j]][yid[i][j]] = true;
79 
80         int ans = hungary();
81         printf("%d
", ans);
82     }
83 }
View Code
原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7818081.html