zoukankan      html  css  js  c++  java
  • zoj 1654(最大二分匹配)

    题目的意思是在一个有空地,墙,和草的棋盘上放机器人,  机器人只能放在空地上, 机器人很强大可以攻击上、下、左、右四个方向, 但是不能穿透墙。 然后求最多能放置多少个机器人使其不互相攻击.

    刚开始拿到题的时候,苦苦思考怎么建图, 连将整个图放大25倍的烂方法想了一遍,最后没能想出正确的解法。 因为对二分图构图以及求解不熟悉。 所以想不出怎样构建图

    首先可以知道的是, 在一个点u选择了另一个点v后, u点就不能选择其他的点,联想到这题在一行中选择了对应一个列的一点,那么这行中的其他点(直接到达的)就不能选了。 方之,对于点v 也是,选择了u点就不能选择其他的点, 对应于一个列上只能选择一个点相应的行。

    然后根据给的图,算出每一行每一列直接到达区域对应的标号。 匈牙利算法求解即可

    Place the Robots

    Time Limit: 5 Seconds      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 (<= 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

     


    Author: XU, Luchuan
    Source: ZOJ Monthly, October 2003

     

    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    using namespace std;
    #define N 55
    
    char g[N][N];
    int gc[N][N],gl[N][N];
    int link1[1300][1300];
    int mark[1300],pre[1300];
    int cnt,cnt1;
    
    int dfs(int s)
    {
        for(int i=0;i<cnt1;i++)
        {
            if(mark[i]==1||link1[s][i]==0) continue;
            mark[i]=1;
            if(pre[i]==-1||dfs(pre[i])==1)
            {
                pre[i]=s;
                return 1;
            }
        }
        return 0;
    }
    
    int main()
    {
        int tt=1;
        int T;
        scanf("%d",&T);
        while(T--)
        {
            memset(gc,0,sizeof(gc));
            memset(link1,0,sizeof(link1));
            memset(gl,0,sizeof(gl));
            memset(pre,-1,sizeof(pre));
            int n,m;
            scanf("%d%d",&n,&m);
            for(int i=0;i<n;i++)
                for(int j=0;j<m;j++)
                    cin>>g[i][j];
            cnt=1,cnt1=1;
            for(int i=0;i<n;i++)
            {
                int flag=0;
                g[i][m]='#';
                for(int j=0;j<=m;j++)
                {
                    if(g[i][j]=='*') continue;
                    if(g[i][j]=='#')
                    {
                        if(flag==0) continue;
                        cnt++;
                        flag=0;
                    }
                    else
                    {
                        flag=1;
                        gc[i][j]=cnt;
                    }
                }
            }
            for(int j=0;j<m;j++)
            {
                int flag=0;
                g[n][j]='#';
                for(int i=0;i<=n;i++)
                {
                    if(g[i][j]=='*') continue;
                    if(g[i][j]=='#')
                    {
                        if(flag==0) continue;
                        cnt1++;
                        flag=0;
                    }
                    else
                    {
                        flag=1;
                        gl[i][j]=cnt1;
                    }
                }
            }
            
            for(int i=0;i<n;i++)
                for(int j=0;j<m;j++)
                {
                    if(gc[i][j]!=0)
                    {
                        link1[ gc[i][j] ][ gl[i][j] ]=1;
                    }
                }
            int sum=0;
            for(int i=1;i<cnt;i++)
            {
                memset(mark,0,sizeof(mark));
                sum += dfs(i);
            }
            printf("Case :%d\n",tt++);
            printf("%d\n",sum);
        }
        return 0;
    }
  • 相关阅读:
    关于MySQL数据库优化的部分整理
    PHP跨域form提交
    px、dp和sp,这些单位有什么区别?
    301和302 Http状态有啥区别?
    PHP HTTP请求
    php的http_build_query使用
    nginx ssi 模块
    MongoDB学习笔记(一) MongoDB介绍及安装(摘)
    Django Admin 录入中文错误解决办法
    关于python字符串连接的操作
  • 原文地址:https://www.cnblogs.com/chenhuan001/p/3013250.html
Copyright © 2011-2022 走看看