[ZOJ1654]Place the Robots

Description
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.
有一个N*M(N,M<=50)的棋盘,棋盘的每一格是三种类型之一:空地、草地、墙。机器人只能放在空地上。在同一行或同一列的两个机器人,若它们之间没有墙,则它们可以互相攻击。问给定的棋盘,最多可以放置多少个机器人,使它们不能互相攻击。

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

二分图最大匹配,这题和[Usaco2005 Jan]Muddy Fields泥泞的牧场十分类似,但是本题多了个草地,其实也没啥大碍。对于不是墙的点,把他们连成联通块;对于空地,连边;至于匹配的时候,只要某个编号中曾出现过空地,都是可以做匹配的

/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline int read(){
    int x=0,f=1;char ch=getchar();
    for (;ch<'0'||ch>'9';ch=getchar())    if (ch=='-')    f=-1;
    for (;ch>='0'&&ch<='9';ch=getchar())  x=(x<<1)+(x<<3)+ch-'0';
    return x*f;
}
inline void print(int x){
    if (x>=10)     print(x/10);
    putchar(x%10+'0');
}
const int N=50;
int pre[N*N+10],now[(N*N>>1)+10],child[N*N+10];
char map[N+10][N+10];
int A[N+10][N+10],B[N+10][N+10],path[(N*N>>1)+10];
bool use[(N*N>>1)+10],flag[(N*N>>1)+10];
int n,m,tot,Acnt,Bcnt,ans;
void join(int x,int y){pre[++tot]=now[x],now[x]=tot,child[tot]=y;}
bool check(int x){
    for (int p=now[x],son=child[p];p;p=pre[p],son=child[p]){
        if (use[son])   continue;
        use[son]=1;
        if (path[son]<0||check(path[son])){
            path[son]=x;
            return 1;
        }
    }
    return 0;
}
void init(){
    tot=Acnt=Bcnt=ans=0;
    memset(now,0,sizeof(now));
    memset(flag,0,sizeof(flag));
    memset(path,-1,sizeof(path));
}
int main(){
    int Data=read();
    for (int I=1;I<=Data;I++){
        init();
        n=read(),m=read();
        for (int i=1;i<=n;i++)   scanf("%s",map[i]+1);
        for (int i=1;i<=n;i++)
            for (int j=1;j<=m;j++)
                if (map[i][j]!='#'){
                    A[i][j]=j>1&&map[i][j-1]!='#'?A[i][j-1]:++Acnt;
                    B[i][j]=i>1&&map[i-1][j]!='#'?B[i-1][j]:++Bcnt;
                    flag[A[i][j]]|=map[i][j]=='o';
                }
        for (int i=1;i<=n;i++)   for (int j=1;j<=m;j++)   if (map[i][j]=='o') join(A[i][j],B[i][j]);
        for (int i=1;i<=Acnt;i++){
            if (!flag[i])   continue;
            memset(use,0,sizeof(use));
            if (check(i))   ans++;
        }
        printf("Case :%d\n%d\n",I,ans);
    }
    return 0;
}

posted @ 2018-02-07 10:22  Wolfycz  阅读(222)  评论(0编辑  收藏  举报