• 构造二分图匹配


    Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall.

    A blockhouse is a small castle that has four openings through which to shoot. The four openings are facing North, East, South, and West, respectively. There will be one machine gun shooting through each opening.

    Here we assume that a bullet is so powerful that it can run across any distance and destroy a blockhouse on its way. On the other hand, a wall is so strongly built that can stop the bullets.

    The goal is to place as many blockhouses in a city as possible so that no two can destroy each other. A configuration of blockhouses is legal provided that no two blockhouses are on the same horizontal row or vertical column in a map unless there is at least one wall separating them. In this problem we will consider small square cities (at most 4x4) that contain walls through which bullets cannot run through.

    The following image shows five pictures of the same board. The first picture is the empty board, the second and third pictures show legal configurations, and the fourth and fifth pictures show illegal configurations. For this board, the maximum number of blockhouses in a legal configuration is 5; the second picture shows one way to do it, but there are several other ways.



    Your task is to write a program that, given a description of a map, calculates the maximum number of blockhouses that can be placed in the city in a legal configuration.

    InputThe input file contains one or more map descriptions, followed by a line containing the number 0 that signals the end of the file. Each map description begins with a line containing a positive integer n that is the size of the city; n will be at most 4. The next n lines each describe one row of the map, with a '.' indicating an open space and an uppercase 'X' indicating a wall. There are no spaces in the input file.
    OutputFor each test case, output one line containing the maximum number of blockhouses that can be placed in the city in a legal configuration.
    Sample Input
    4
    .X..
    ....
    XX..
    ....
    2
    XX
    .X
    3
    .X.
    X.X
    .X.
    3
    ...
    .XX
    .XX
    4
    ....
    ....
    ....
    ....
    0
    Sample Output
    5
    1
    5
    2
    4

    题意 : 类似八皇后,同一行或同一列不能有相一起出现的,除非中间可以隔一个障碍物,问最多可以放多少个物品
    思路分析:处理出来行有多少个连通的块,列有多少个连通的块。分别标号处理,对于行列同一个位置,若其不为 -1,则将行列此位置连上边,最后跑一个二分图匹配就可以了。
    代码示例:

    int n;
    char mp[10][10];
    int h[10][10], l[10][10];
    int h1, l1;
    int f[100][100];
    
    void init(){
        h1 = l1 = 1;
        memset(h, -1, sizeof(h));
        memset(l, -1, sizeof(l));
         
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                if (mp[i][j] == '.' && h[i][j] == -1){
                    for(int k = j; k <= n && (mp[i][k] == '.'); k++){
                        h[i][k] = h1;
                    }
                    h1++; 
                }
            }
        }    
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                if (mp[j][i] == '.' && l[j][i] == -1){
                    for(int k = j; k <= n &&(mp[k][i] == '.'); k++){
                        l[k][i] = l1;
                    }
                    l1++;
                }
            }
        }
    }
    
    int pt[100], used[100];
    
    bool find(int x){
        for(int i = 1; i < l1; i++){
            if (f[x][i] && !used[i]){
                used[i] = 1;
                if (pt[i] == 0 || find(pt[i])){
                    pt[i] = x;
                    return true;
                }
            }
        }
        return false;
    }
    
    int main() {
        //freopen("in.txt", "r", stdin);
        //freopen("out.txt", "w", stdout);
        
        while(~scanf("%d", &n) && n){
            for(int i = 1; i <= n; i++){
                scanf("%s", mp[i]+1);
            }        
            init();
            memset(f, 0, sizeof(f));
            memset(pt, 0, sizeof(pt));
            for(int i = 1; i <= n; i++){
                for(int j = 1; j <= n; j++){
                    if (mp[i][j] == '.'){
                        int a = h[i][j], b = l[i][j];
                        f[a][b] = 1;
                    }
                }
            }
            int ans = 0;
            for(int i = 1; i < h1; i++){
                memset(used, 0, sizeof(used));
                if (find(i)) ans++;
            }
            printf("%d
    ", ans);
        }
        return 0;
    }
    
    东北日出西边雨 道是无情却有情
  • 相关阅读:
    Jenkins远程部署应用
    Centos7搭建Jenkins
    Centos7安装Docker
    Centos7安装Maven
    Centos7安装jdk
    由object元素引出的事件注册问题和层级显示问题
    ios中input输入无效
    手册
    CSS 清楚浮动总结
    JS 创建对象总结
  • 原文地址:https://www.cnblogs.com/ccut-ry/p/9030202.html
Copyright © 2020-2023  润新知