zoukankan      html  css  js  c++  java
  • ZOJ1654 Place the Robots

    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

    solution

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

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

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

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

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

    #include<bits/stdc++.h>
    using namespace std;
    int Map1[55][55], Map2[55][55], Map[1300][1300];
    int white[2505][2], cnt = 0;
    unsigned int used[1300];
    int match[1300];
    int pos1, pos2;
    
    bool Dfs(int u)
        {
            for(int v = 1; v <= pos2; ++ v)
                {
                    if(Map[u][v] && !used[v])
                        {
                            used[v] = 1;
                            if(!match[v] || Dfs(match[v]))
                                {
                                    match[v] = u;
                                    return true;
                                }
                        }
                }
            return false;
        }
    
    int main()
    {
        int T;
        int n, m, cases = 1;
        char s[55];
        scanf("%d", &T);
        for(;T;T --, ++ cases)
            {
                cnt = 0;
                memset(Map, 0, sizeof(Map));
                memset(match, 0, sizeof(match));
                scanf("%d%d", &n, &m);
                for(int i = 1; i <= n; ++ i)
                    {
                        scanf("%s", s + 1);
                        for(int j = 1; j <= m; ++ j)
                            switch (s[j])
                                {
                                    case 'o' : Map2[i][j] = Map1[i][j] = 0; white[++ cnt][0] = i; white[cnt][1] = j;break;
                                    case '*' : Map2[i][j] = Map1[i][j] = -1; break;
                                    case '#' : Map2[i][j] = Map1[i][j] = -2; break;
                                }
                    }
                pos1 = 0;
                for(int i = 1; i <= n; ++ i)
                    for(int j = 1; j <= m; ++ j)
                        if(Map1[i][j] == 0 || Map1[i][j] == -1)
                            if(Map1[i][j - 1] > 0)    Map1[i][j] = Map1[i][j - 1];
                            else Map1[i][j] = ++ pos1;
                pos2 = 0;
                for(int i = 1; i <= m; ++ i)
                    for(int j = 1; j <= n; ++ j)
                        if(Map2[j][i] == 0|| Map2[j][i] == -1)
                            if(Map2[j - 1][i] > 0)    Map2[j][i] = Map2[j - 1][i];
                            else Map2[j][i] = ++ pos2;
                for(int i = 1; i <= cnt; ++ i)
                    Map[Map1[white[i][0]][white[i][1]]][Map2[white[i][0]][white[i][1]]] = 1;
                int ans = 0;
                for(int i = 1; i <= pos1; ++ i)
                    {
                        memset(used, 0, sizeof(used));
                        if(Dfs(i)) ans ++;
                    }
                printf("Case :%d
    %d
    ", cases, ans);
            }
    }
  • 相关阅读:
    干货 | 玩转云文件存储——利用CFS实现web应用的共享访问
    干货 | 4步带你完成私有云盘搭建
    是我们控制着技术,还是技术控制着我们?
    如何辨别开发者等级?
    云托管,边缘物理计算&托管物理计算,你所需要了解的……
    干货 | 调用AI api 实现网页文字朗读
    容器技术的未来——京东云技术专访
    隐藏的历史-是什么成就了今天的硅谷?
    ffmpeg windows下编译安装
    比较和打补丁工具
  • 原文地址:https://www.cnblogs.com/2020pengxiyue/p/9281056.html
Copyright © 2011-2022 走看看