http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=654
Title Description:
Robert is a well-known engineer. One day, his boss assigned him a task. The background of the task is: given a
A map of the size of MXN, the map is made up of squares, there are 3 kinds of squares in the map-walls, meadows and open space, his boss wants
Can place as many robots as possible in the map. Each robot is equipped with a laser gun that can be in four directions at the same time (up, down,
Left and right) shot. The robot stays in the square where it was originally placed, does not move, and then shoots in four directions. Excited
Lasers emitted by light guns can penetrate the grass, but not through the walls. Robots can only be placed in open spaces. Of course, the boss doesn't want
Robots attack each other, that is, two robots cannot be placed on the same line (horizontal or vertical) unless there is a wall between them
The grid opens.
Given a map, your program needs to output the maximum number of robots that can be placed in the map.
Ideas:
Explain in a sample example
In the prototype of the problem, the grass, the wall, the information is not the subject of concern, the concern is only the connection between open space and open space. Therefore, it is natural to think of the following simple model: The space as the apex, in the conflict between the open space between the edge.
Put all the vacant spaces in numbers and get a graph:
Connect all the conflicting open spaces with the edges to get the picture (b):
As a result, the problem translates into the maximum set of independent sets for the graph: the maximum vertex set, and all vertices in the collection are not connected (that is, non-conflicting). But the maximum point independent set problem is a NP problem, and no effective algorithm can solve it.
————————————————————————————————————————————————————————————————————————
The contiguous area of each row separated by walls and containing open spaces is called "blocks". Obviously, in one block, you can put at most one robot. Make these blocks numbered, as shown in 7.25 (a). It should be explained that the last line, that is, the 4th row has two vacant lots, but there is no wall between the two vacant spaces, only the grass, so the two vacant spaces should belong to the same "block". Similarly, the vertical direction of the block is also numbered, 7.25 (b) is shown.
Consider each transverse block as a vertex in the vertex set X in the two-part graph, and the vertical block as a vertex in the set Y, and if two blocks have a public space (note that there is a maximum of one public space per two blocks), the edges are connected between them. For example, the horizontal block 2 and the vertical Block 1 have a common open space, i.e. (2, 0), so there is an edge between the vertex 2 and the Y set in the X collection. In this way, the problem is transformed into a two-part diagram, as shown in 7.25 (c). Since each edge represents an open space (i.e. a horizontal block and a public open space of a vertical block), there must be a public vertex between the open spaces that are in conflict. For example, the Edge (x1, y1) indicates that the open space (0, 0), The Edge (x2, y1) represents the open space (2, 0), and the robot cannot be placed in both spaces. So the problem turns into finding the largest set of edges with no public vertices in the two chart, which is the maximum matching problem.
The result of the construction of the sample given above:
Hungarian Algorithm O (| e| sqrt (| v|) )
0MS 372KB
#include <iostream>#include <cstdio>#include <cstring>#include <vector>Const intmaxn=2550;using namespace STD;intN,m;intNx,ny;The number of blocks in the horizontal direction, the number of blocks in the vertical directionCharpic[ -][ -];//Mapintxs[ -][ -],ys[ -][ -];The number of the block in the horizontal direction, the number of the block in the vertical direction vector<int>G[MAXN];//g[i] Indicates the right point to the left point IintFROM[MAXN];//from[y] Represents the x Vertex//cy array that matches YiintTot//maximum number of matchesBOOLUSE[MAXN];//intFlagBOOLMatchintx) { for(intI=0; I<g[x].size (); ++i) {if(!use[g[x][i]]) {use[g[x][i]]=true;if(from[g[x][i]]==-1|| Match (From[g[x][i])) {from[g[x][i]]=x;return true; } } }return false;}intHungary () {tot=0;memset(From,255,sizeof(from)); for(intI=1; i<=nx;++i) {memset(Use,0,sizeof(use));if(Match (i)) ++tot; }returnTot;}intMain () {intTCin>>T; for(intcas=1; cas<=t;++cas) {printf("Case:%d\n", CAs);scanf("%d%d", &n,&m); for(intI=0; i<n;++i)scanf('%s ', Pic[i]); nx=ny=0;memset(XS,0,sizeof(XS));memset(Ys,0,sizeof(YS)); for(intI=0; i<n;++i) {flag=0; for(intj=0; j<m;++j) {if(pic[i][j]==' O '){if(!flag) ++nx; flag=1; Xs[i][j]=nx; }Else if(pic[i][j]==' # ') flag=0; } } for(intj=0; j<m;++j) {flag=0; for(intI=0; i<n;++i) {if(pic[i][j]==' O '){if(!flag) ++ny; flag=1; Ys[i][j]=ny; }Else if(pic[i][j]==' # ') flag=0; } } for(intI=1; i<=nx;++i) g[i].clear (); for(intI=0; i<n;++i) { for(intj=0; j<m;++j) {if(Xs[i][j]&&ys[i][j]) g[xs[i][j]].push_back (Ys[i][j]); } }printf("%d\n", Hungary ()); }return 0;}
ZOJ1654. Place the robots placement robot-maximum matching of two graphs (Hungary algorithm)