ZOJ 1654 Place the Robots (maximum match)

Source: Internet
Author: User

Robert is a famous engineer. One day he is 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 is 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 is placed in an Empty block. Surely the boss would not want to see one robot hurting another. In the other words, the robots must not being placed in one line (horizontally or vertically) unless there is a Wall between them .

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


Input


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

For each test case, the first line contains integers m and n (1<= m, n <=50) which is 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, and the format: "Case:id" where ID is the test Case number, Counting from 1. The second line just output the maximum number of robots the can is 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

Test instructions: The robot can attack everything that is in line with it, only ' o ' can put the robot, ' # ' means the wall, can block the robot's attack (meaning that the wall can be placed between the robot), you need to find out the n*m matrix can put how many robots

Idea: Maximum independent set, unfortunately there is no algorithm to find the maximum independent set.

Then we change a train of thought, previously did POJ3041, can do this problem similarly, but the wall in the subject provides the difficulty for our mark, then I can use the XS array as the X set (indicating the position of the ' o ' in each row, if incompatible then mark the same number), the same as a column of the YS array! Corresponding down, on each ' o ' connected two sets of points formed edge, we found that each side of the subject can not have a common point, so the conversion to the minimum side coverage problem, just is 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 NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

ZOJ 1654 Place the Robots (maximum match)

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.