HDU1045 Fire Net —— 二分圖最大匹配
阿新 • • 發佈:2017-11-11
tin pict 直接 sed esc tom 一行 resp logs
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.
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.
題目鏈接: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
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.
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 Zhejiang University Local Contest 2001
題解:
此題可以直接枚舉,但如果數據更大的話,就需要用二分圖來解了:
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\n", ans); 82 } 83 }View Code
HDU1045 Fire Net —— 二分圖最大匹配