zoukankan      html  css  js  c++  java
  • ZOJ 1654 Place the Robots 二分图 最大匹配

    $ ightarrow $ 戳我进ZOJ原题

    Place the Robots


    Time Limit: 5 Seconds $ quad $ 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 (le 11) $ which is the number of test cases.
     
    For each test case, the first line contains two integers $ m $ and $ n (1 le m, n le 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
    

    Author: XU, Luchuan

    Source: ZOJ Monthly, October 2003
     

    题目大意

    • 有一个 $ N imes M (N,M le 50) $ 的棋盘

    • 棋盘的每一格是三种类型之一:空地、草地、墙。

    • 机器人只能放在空地上。

    • 在同一行或者同一列的两个机器人,若他们之间没有墙,则他们可以互相攻击。

    • 问给定的棋盘,最多可以放置多少个机器人,使它们不能互相攻击。

    pic1

    题解

    • 把每一行、每一列被墙隔开,并且包含空地的连续区域称为“块”。

    • 显然,在一个块之中,最多只能放一个机器人,把这些块编上号。

    • 把每个横向块看作左部的点,竖向块看作右部的点

    • 若两个块有公共的空地,则在它们之间连边

    • 求二分图的最大匹配,即可得到答案

    pic2

    代码

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    using namespace std;
    #define N 55
    #define M 2505
    vector<int>e[M];
    int T,n,m,ans;
    bool f;
    char mp[N][N];
    int cntx,cnty,x[N][N],y[N][N]; 
    int link[M]; bool vis[M];
    bool dfs(int u){
    	for(int i=0;i<e[u].size();++i)
    		if(!vis[e[u][i]]){
    			int v=e[u][i];
    			vis[v]=1;
    			if(!link[v]||dfs(link[v])){
    				link[v]=u;
    				return 1;
    			}
    		}
    	return 0;
    }
    int main(){
    	scanf("%d",&T);
    	for(int cases=1;cases<=T;++cases){
    		memset(x,0,sizeof(x)); 
    		memset(y,0,sizeof(y));
    		memset(link,0,sizeof(link));
    		cntx=cnty=ans=0;
    		scanf("%d %d",&n,&m);
    		for(int i=1;i<=n;++i){
    			f=0; scanf("%s",mp[i]+1);
    			for(int j=1;j<=m;++j)
    				if(mp[i][j]=='o'){
    					if(!f) ++cntx; f=1;
    					x[i][j]=cntx;
    				}else if(mp[i][j]=='#') f=0;
    		}
    		for(int j=1;j<=m;++j){
    			f=0;
    			for(int i=1;i<=n;++i)
    				if(mp[i][j]=='o'){
    					if(!f) ++cnty; f=1;
    					y[i][j]=cnty;
    				}else if(mp[i][j]=='#') f=0;
    		}
    		for(int i=1;i<=cntx;++i) e[i].clear();
    		for(int i=1;i<=n;++i)
    			for(int j=1;j<=m;++j)
    				if(x[i][j]&&y[i][j]) 
    					e[x[i][j]].push_back(y[i][j]);
    		for(int i=1;i<=cntx;++i){
    			memset(vis,0,sizeof(vis)); 
    			if(dfs(i)) ++ans;
    		}
    		printf("Case :%d
    %d
    ",cases,ans);
    	}
    	return 0;
    }
    
  • 相关阅读:
    CentOS 163 yum 源使用
    RedHat Enterprise Linux 5 安装TFTP服务器和NFS服务器
    Hadoop压缩SNAPPY算法安装
    linux 修改hostname 方法小结
    svnserver配置文件详解
    RedHat启动时sendmail启动慢的解决
    HBase 系统架构
    CentOS 域名解析 配置问题
    yum 安装 gcc
    Missing indirectly referenced artifact com.sun:tools:jar:1.5.0:system 错误
  • 原文地址:https://www.cnblogs.com/PotremZ/p/ZOJ1654.html
Copyright © 2011-2022 走看看