码迷,mamicode.com
首页 > 其他好文 > 详细

ZOJ 1654 Place the Robots(最大匹配)

时间:2017-05-22 15:12:21      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:最大匹配   friends   cte   round   this   represent   possible   each   cst   

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

题意:机器人能攻击跟它所在同一行跟列的全部东西,仅仅有‘o‘才干放机器人,‘#‘表示墙壁,能挡住机器人的攻击(意味着墙壁之间能放机器人),要你求出n*m的矩阵上能放多少机器人

思路:最大独立集。可惜眼下没有不论什么算法能求出最大独立集。

那么我们换一个思路,之前做过POJ3041,能够类似地做这道题,可是本题中的墙壁为我们的标记提供了难度。那么我么能够用xs数组看做X集合(表示每行中的‘o‘的位置。假设不相容则标记为同一个数),同理做一个列的ys数组!

相应下来。在每个‘o‘上连接两集合的点形成边,我们发现符合题目的每条边之间不能有公共点,所以就转化为了最小边覆盖的问题,刚好就是最大匹配!

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int N = 55;
const int maxn=2600;

int link[maxn][maxn];
int xs[N][N],ys[N][N];
int col[maxn],vis[maxn];
int mx,my;

int n,m;

int match(int x)
{
    int i;
    for(i=1;i<=my;i++){
        if(link[x][i]&&!vis[i])
        {
            vis[i]=1;
            if(!col[i]||match(col[i]))
            {
                col[i]=x;
                return 1;
            }
        }
    }
    return 0;
}

char str[N][N];

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.cpp","r",stdin);
    freopen("out.cpp","w",stdout);
    #endif // ONLINE_JUDGE
    int t;
    scanf("%d",&t);
    int cas=1;
    while(t--)
    {
        int cnt=1;
        scanf("%d %d",&n,&m);
        memset(xs,0,sizeof(xs));
        memset(ys,0,sizeof(ys));
        getchar();
        for(int i=0;i<n;i++)
        {
            scanf("%s",str[i]);
            for(int j=0;j<m;j++)
            {
                if(str[i][j]==‘o‘)
                {
                    xs[i][j]=cnt;
                }

                else if(str[i][j]==‘#‘)
                    cnt++;
            }
            cnt++;
        }
        int maxx=cnt;
        mx=cnt;
        cnt=1;
        for(int j=0;j<m;j++)
        {
            for(int i=0;i<n;i++)
            {
                if(str[i][j]==‘o‘)
                    ys[i][j]=cnt;
                else if(str[i][j]==‘#‘)
                    cnt++;
            }
            cnt++;
        }
        my=cnt;
        memset(link,0,sizeof(link));
        memset(col,0,sizeof(col));
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(str[i][j]==‘o‘)
                {
                    link[xs[i][j]][ys[i][j]]=1;
                }
            }
        }

        int tot=0;
        for(int i=1;i<=mx;i++)
        {
            memset(vis,0,sizeof(vis));
            if(match(i))tot++;
        }
        printf("Case :%d\n",cas++);
        printf("%d\n",tot);
    }
    return 0;
}


ZOJ 1654 Place the Robots(最大匹配)

标签:最大匹配   friends   cte   round   this   represent   possible   each   cst   

原文地址:http://www.cnblogs.com/claireyuancy/p/6889001.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!