HDU

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

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

********************************************

题意:n乘n 的矩阵,一般同一行同一列不能放两个'O',如果有'X'分割开来则可以,问你在'.'处最多可以放多少个'O'。

分析:二分匹配。

AC代码:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<math.h>
 4 #include<queue>
 5 #include<algorithm>
 6 #include<time.h>
 7 #include<stack>
 8 #include<vector>
 9 using namespace std;
10 #define N 1200
11 #define INF 0x3f3f3f3f
12 
13 int vv[N],x,y,n;
14 char maps[10][10];
15 int vis[N][N],v[N];
16 
17 struct node
18 {
19     int x,y;
20 } a[N][N];
21 
22 int Hungary(int u)///匈牙利算法匹配
23 {
24     for(int i=1; i<=y; i++)
25         if(vis[u][i]&&!v[i])
26         {
27             v[i]=1;
28             if(!vv[i]||Hungary(vv[i]))
29             {
30                 vv[i]=u;
31                 return 1;
32             }
33         }
34 
35     return 0;
36 }
37 
38 int main()
39 {
40     int i,j;
41 
42     while(scanf("%d", &n),n)
43     {
44         for(i=0; i<n; i++)
45             scanf("%s", maps[i]);
46 
47         x=0,y=0;
48         memset(vis,0,sizeof(vis));
49 
50         for(i=0; i<n; i++)
51             for(j=0; j<n; j++)
52             {
53                 ///把图分割,以相连的‘.’为行和列重新分配编号
54                 if(maps[i][j]=='.')
55                 {
56                     if(j==0||maps[i][j-1]=='X')
57                         x++;
58                     a[i][j].x=x;
59                 }
60                 if(maps[j][i]=='.')
61                 {
62                     if(j==0||maps[j-1][i]=='X')
63                         y++;
64                     a[j][i].y=y;
65                 }
66             }
67 
68         for(i=0; i<n; i++)
69             for(j=0; j<n; j++)
70                 if(maps[i][j]=='.')
71                 {
72                     int u=a[i][j].x;
73                     int v=a[i][j].y;
74                     vis[u][v]=1;///用行匹配列
75                 }
76 
77         int ans=0;
78         memset(vv,0,sizeof(vv));
79         for(i=1; i<=x; i++)
80         {
81             memset(v,0,sizeof(v));
82             if(Hungary(i)==1)
83                 ans++;
84         }
85 
86         printf("%d
", ans);
87     }
88     return 0;
89 }
原文地址:https://www.cnblogs.com/weiyuan/p/5799363.html