zoj1654 Place the Robots

Source: Internet
Author: User

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 (VE)


Detailed description of the topic transferred from http://m.blog.csdn.net/blog/u014141559/44409255

#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
#include <stack>
#pragma COMMENT (linker, "/stack:102400000,102400000")
#define PI ACOs (-1.0)
#define EPS 1e-6
#define INF (1&LT;&LT;24)
using namespace Std;
int m,n;


Char map[55][55]; Map
int from[2555]; From[y] represents the x vertex that matches Yi
BOOL use[2555];
int xs[55][55],ys[55][55]; The number of the block in the horizontal direction, the number of the block in the vertical direction
vector<int>g[2555]; G[i] Indicates the right point to the left point I
int nx,ny; The number of blocks in the horizontal direction, the number of blocks in the vertical direction


BOOL Match (int x)
{
for (int i=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;
}
int Hungary ()
{
int tot=0;
Memset (From,255,sizeof (from));
for (int i=1;i<=nx;i++)
{
memset (use,0,sizeof (use));
if (Match (i)) tot++;
}
return tot; Maximum number of matches
}
/* Binary Graph Max match */
/* Hungarian Algorithm */


int main ()
{
int t,k;
scanf ("%d", &t);
for (k=1;k<=t;k++)
{
scanf ("%d%d", &m,&n);
GetChar ();
int i,j;
for (i=0;i<m;i++)
{
Gets (Map[i]);
}
/* for (i=0;i<m;i++)
{
Puts (Map[i]);
}*/


nx=ny=0;
memset (xs,0,sizeof (XS));
memset (Ys,0,sizeof (YS));
int flag;


for (i=0;i<m;i++)//Landscape numbering
{
flag=0;
for (j=0;j<n;j++)
{
if (map[i][j]== ' O ')
{
if (flag!=1) nx++;
flag=1;
Xs[i][j]=nx;
}
else if (map[i][j]== ' # ') flag=0;
}
}
for (j=0;j<n;j++)//Portrait numbering
{
flag=0;
for (i=0;i<m;i++)
{
if (map[i][j]== ' O ')
{
if (flag!=1) ny++;
flag=1;
Ys[i][j]=ny;
}
else if (map[i][j]== ' # ') flag=0;
}
}
/*x,y direction of the number of the two points of the plot side point */
for (i=0;i<=nx;i++) g[i].clear ();


for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
if (Xs[i][j]&&ys[i][j])//Two numbers are crossed,
{
G[xs[i][j]].push_back (Ys[i][j]); Have a relationship
}
}
}


printf ("Case:%d\n", K);
printf ("%d\n", Hungary ());
}
return 0;
}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

zoj1654 Place the Robots

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.