ZOJ 1654 Place the Robots (maximum match), zojrobots

Source: Internet
Author: User

ZOJ 1654 Place the Robots (maximum match), zojrobots

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 your robots as possible in the map. each robot held a laser weapon which cocould shoot to four direly LY (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 cocould pass the grid of Grass, but cocould not pass the grid of Wall. A robot cocould only be placed in an Empty block. surely the boss wocould 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
O ***
*###
Oo # o
* ** O
4
# Ooo
O # oo
Oo # o
***#


Sample Output

Case: 1
3
Case: 2

5

A: A robot can attack everything in the same row and column as it. Only 'O' can hold the robot. '#' indicates the wall, robots can be blocked (meaning robots can be placed between walls). How many robots can be placed on the n * m matrix?

Idea: the largest independent set. Unfortunately, no algorithm can be used to find the largest independent set.

So let's change our thinking. We have done POJ3041 before, and we can do this in a similar way, but the walls in this question provide difficulties for our marking, so I can use the xs array as the X set (indicating the location of 'o' in each row, and if it is incompatible, it will be marked as the same number). Similarly, we can create a column's ys array! Correspondingly, we connect the points of the Two sets on each 'O' to form an edge. We find that there cannot be any common points between each edge that meets the question, so it is converted to the problem of least edge coverage, exactly the maximum match!

AC code:

#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;}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.