Fire Net
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3676 Accepted Submission(s): 2114
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
Source
二分图构图法:
一行变多行,一列变多列
一行变多行,一列变多列
###
###
###
###
###
上面是一个4*4的方格,方格内的###表示墙,我们要在表格内没有墙的地方建立碉堡,而且要保证任何两个碉堡之间互相不能攻击,问最多能建多少个碉堡?是否感觉像第一个题呢?如果我们向第一个题那样建图,那么最后求出来的最大匹配也就是行和列的匹配。而且这个匹配满足了所有匹配都是不同行不同列(匹配本身的性质就是每个点至多属于匹配中的某个边)。但是这样的建图的话,我们墙怎么处理? 有墙的地方就相当于把这一行和这一列分成了两行,两列。 例如
###
等价于
还是一行
###
等价于
###
###
和
一行变成了两行
对就是这么分的。
###
###
原图
(因为有了墙所以第一行变为两行)
(因为上面有了二行固只能从第三行开始)
(因为第一列有了墙,固列数增加为5)
(第3,4列有了墙,固列数增加到了6和7)
一行变多行,一列变多列后的图
然后我们按照这个编号见图即可
1 1
2 2
3 3
4 4
5 5
6 6
7
对这个图求二分图最大匹配即可。这个问题也解决了!
1,1
1,2
###
1,4
###
2,2
2,3
2,4
3,1
3,2
###
###
4,1
###
4,3
4,4
1,1
1,2
###
2,4
###
3,2
3,3
3,4
4,5
4,2
###
###
5,5
###
6,6
6,7
数组不要开小了啊!!!!
code:
1 #include<iostream>
2 using namespace std;
3
4 #define MAXN 500
5
6 char map[MAXN][MAXN];
7 int map1[MAXN][MAXN],map2[MAXN][MAXN]; //存储横纵坐标
8 int map3[MAXN][MAXN]; //存储二分图
9 bool vst[MAXN];
10 int path[MAXN];
11 int dis[MAXN][MAXN];
12 int maxx,maxy; //最大坐标值
13 int n;
14
15 bool dfs(int v)
16 {
17 for(int i=1;i<=maxy;i++)
18 {
19 if(!vst[i]&&map3[v][i])
20 {
21 vst[i]=true;
22 if(path[i]==-1||dfs(path[i]))
23 {
24 path[i]=v;
25 return true;
26 }
27 }
28 }
29 return false;
30 }
31
32 int hungary()
33 {
34 int cnt=0;
35 memset(path,-1,sizeof(path));
36 for(int i=1;i<=maxx;i++)
37 {
38 memset(vst,false,sizeof(vst));
39 if(dfs(i))
40 cnt++;
41 }
42 return cnt;
43 }
44
45 int main()
46 {
47 int i,j;
48 while(~scanf("%d",&n),n)
49 {
50 memset(map1,0,sizeof(map1));
51 memset(map2,0,sizeof(map2));
52 memset(map3,0,sizeof(map3));
53 getchar();
54 //------------------------------------读入数据------------------------------------------------------------------------
55 for(i=1;i<=n;i++)
56 {
57 for(j=1;j<=n;j++)
58 {
59 scanf("%c",&map[i][j]);
60 if(map[i][j]=='X')
61 map1[i][j]=map2[i][j]=-1;
62 }
63 getchar();
64 }
65 //-----------------------------------------------找坐标并赋值----------------------------------------------------------
66 int cnt1=0,cnt2=0;
67 maxx=maxy=0;
68 for(i=1;i<=n;i++)
69 {
70 for(j=1;j<=n;j++)
71 {
72 while(map1[i][j]==-1&&j<=n) //实现定位
73 j++;
74 cnt1++;
75 while(map1[i][j]!=-1&&j<=n)
76 {
77 map1[i][j]=cnt1;
78 if(maxx<cnt1) //记录最大的横坐标
79 maxx=cnt1;
80 j++;
81 }
82 }
83 }
84
85
86 for(j=1;j<=n;j++)
87 {
88 for(i=1;i<=n;i++)
89 {
90 while(map2[i][j]==-1&&i<=n)
91 i++;
92 cnt2++;
93 while(map2[i][j]!=-1&&i<=n)
94 {
95 map2[i][j]=cnt2;
96 if(maxy<cnt2)
97 maxy=cnt2;
98 i++;
99 }
100 }
101 }
102 //--------------------------------------------------构图----------------------------------------------------------
103 for(i=1;i<=n;i++)
104 for(j=1;j<=n;j++)
105 if(map1[i][j]!=-1&&map2[i][j]!=-1)
106 map3[map1[i][j]][map2[i][j]]=1;
107
108
109 printf("%d\n",hungary());
110 }
111 return 0;
112 }
113 /*
114 4
115 ..X.
116 X...
117 ..XX
118 .X..
119 */