zoj 1654 Place the Robots (二分图匹配)

Place the Robots Time Limit: 5 Seconds      Memory Limit: 32768 KB

Robert is a famous engineer. One day he was given a task by his boss. The background of the task was the following:

Given a map consisting of square blocks. There were three kinds of blocks: Wall, Grass, and Empty. His boss wanted to place as many robots as possible in the map. Each robot held a laser weapon which could shoot to four directions (north, east, south, west) simultaneously. A robot had to stay at the block where it was initially placed all the time and to keep firing all the time. The laser beams certainly could pass the grid of Grass, but could not pass the grid of Wall. A robot could only be placed in an Empty block. Surely the boss would not want to see one robot hurting another. In other words, two robots must not be placed in one line (horizontally or vertically) unless there is a Wall between them.

Now that you are such a smart programmer and one of Robert's best friends, He is asking you to help him solving this problem. That is, given the description of a map, compute the maximum number of robots that can be placed in the map.


Input


The first line contains an integer T (<= 11) which is the number of test cases.

For each test case, the first line contains two integers m and n (1<= m, n <=50) which are the row and column sizes of the map. Then m lines follow, each contains n characters of '#', '*', or 'o' which represent Wall, Grass, and Empty, respectively.


Output

For each test case, first output the case number in one line, in the format: "Case :id" where id is the test case number, counting from 1. In the second line just output the maximum number of robots that can be placed in that map.


Sample Input

2
4 4
o***
*###
oo#o
***o
4 4
#ooo
o#oo
oo#o
***#


Sample Output

Case :1
3
Case :2
5


/*
本题运用构建二分图思想
将每一行被墙隔开、且包含空地的连续区域称作“块”(每一列同理)。
在一个块之中,最多只能放一个机器人,把这些块编上号。
把每个横向块看作二部图中顶点集合 X 中的顶点,竖向块看作集合 Y 中的顶点。
由于每条边表示一个空地(即一个横向块和一个竖向块的公共空地),有冲突的空地之间必有公共顶点。
若两个块有公共的空地(注意,每两个块最多有一个公共空地),则在它们之间连边。
所以问题转化为在二部图中找没有公共顶点的最大边集,这就是二分图最大匹配问题。
*/
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define maxn 51*51
using namespace std;

int n,m,ans;          // n-行 m-列 ans-答案
int cntx,cnty,flag;   // cntx-横向块数  cnty-竖向块数
int g[maxn][maxn],mapx[51][51],mapy[51][51];   // g[][]存二分图  mapx[][]-原图对应的横向序号 mapy[][]同理
int cx[maxn],cy[maxn],mk[maxn];  // cx存匹配的cy序号  cy同理  mk标记cy是否匹配
char mapp[51][51];    // 存原图

int path(int u)
{
    int v;
    for(v=1;v<=cnty;v++)
    {
        if(g[u][v]&&!mk[v])
        {
            mk[v]=1;
            if(!cy[v]||path(cy[v]))
            {
                cx[u]=v;
                cy[v]=u;
 //             printf("u:%d v:%d\n",u,v);
                return 1;
            }
        }
    }
    return 0;
}
void maxmarch()
{
    int i,j;
    ans=0;
    memset(cx,0,sizeof(cx));
    memset(cy,0,sizeof(cy));
    for(i=1;i<=cntx;i++)
    {
        if(!cx[i])
        {
            memset(mk,0,sizeof(mk));
            ans+=path(i);
        }
    }
}
int main()
{
	int i,j,k,t;
	scanf("%d",&t);
	for(k=1;k<=t;k++)
	{
	    memset(g,0,sizeof(g));
	    memset(mapp,0,sizeof(mapp));
		scanf("%d%d",&n,&m);
		for(i=1;i<=n;i++)
        {
            getchar();
            for(j=1;j<=m;j++)
            {
                scanf("%c",&mapp[i][j]);
            }
        }
        cntx=0;
        for(i=1;i<=n;i++)         // 将X块抽象出来
        {
            flag=0;
            for(j=1;j<=m;j++)
            {
                if(mapp[i][j]=='o')
                {
                    if(flag==0)
                    {
                        flag=1;
                        cntx++;
                    }
                    mapx[i][j]=cntx;
                }
                else if(mapp[i][j]=='#')  flag=0;
            }
        }
        cnty=0;
        for(j=1;j<=m;j++)         // 将Y块抽象出来
        {
            flag=0;
            for(i=1;i<=n;i++)
            {
                if(mapp[i][j]=='o')
                {
                    if(flag==0)
                    {
                        flag=1;
                        cnty++;
                    }
                    mapy[i][j]=cnty;
                }
                else if(mapp[i][j]=='#')  flag=0;
            }
        }
        for(i=1;i<=n;i++)          // 对原图中空地操作 连边构建二分图
        {
            for(j=1;j<=m;j++)
            {
                if(mapp[i][j]=='o')
                {
                    g[mapx[i][j]][mapy[i][j]]=1;
                }
            }
        }
        maxmarch();          // dfs求最大匹配模板函数 就不赘述了
        printf("Case :%d\n",k);
        printf("%d\n",ans);
	}
	return 0;
}


你可能感兴趣的:(zoj 1654 Place the Robots (二分图匹配))