• hdoj1045--Fire Net(二分图 转化 )


    Fire Net

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 8620    Accepted Submission(s): 4976


    Problem Description
    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.
     
    Input
    The 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.
     
    Output
    For 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
     
    Source
     
    Recommend
    We have carefully selected several similar problems for you:  1175 1067 1258 1053 1026 
     
    二分图行列匹配, 主要是处理过程;
    #include <cstdio>
    #include <cstring>
    using namespace std;
    char Ap[5][5];
    int Gra[5][5], vis[5], dis[5];
    int dx[5], dy[5], left[5], right[5];
    int n;
    struct Node{
        int x, y;
    }a[5][5];
    void Printf(){
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                printf("%c", Ap[i][j]);
            }
            printf("
    ");
        }
    }
    int x, y;
    bool Search(int a){
        for(int i = 1; i <= y; i++){
            if(Gra[a][i] && !vis[i]){
                vis[i] = 1;
                if(!dis[i] || Search(dis[i])){
                    dis[i] = a; 
                    return true;
                }
            }
        }
        return false;
    }
    int main(){
        while(scanf("%d", &n) != EOF && n){
            for(int i = 0; i < n; i++)
            {
                getchar(); 
                for(int j = 0; j < n; j++)
                    scanf("%c", &Ap[i][j]);
            }
            x = y = 0;
            for(int i = 0; i < n; i++)   //行列匹配; 
                for(int j = 0; j < n; j++)
                {
                    if(Ap[i][j] == '.')  
                    {
                        if(j == 0 || Ap[i][j-1] == 'X'){
                            x++;        /***********************/
                        }
                        a[i][j].x = x;  
                    }
                    if(Ap[j][i] == '.')
                    {
                        if(j == 0 || Ap[j-1][i] == 'X'){
                            y++;       /***********************/
                        }
                        a[j][i].y = y; 
                    }
                }
            memset(Gra, 0, sizeof(Gra));
            for(int i = 0; i < n; i++)
                for(int j = 0; j < n; j++){
                    if(Ap[i][j] == '.'){
                        int u = a[i][j].x;
                        int v = a[i][j].y;
                        Gra[u][v] = 1;
                    }
                }
            int ans = 0;
            memset(dis, 0, sizeof(dis));
            for(int i = 1; i <= x; i++)
            {
                memset(vis, 0, sizeof(vis));
                if(Search(i))
                    ans++;
            }
            printf("%d
    ", ans); 
        }
        return 0;
    } 

    暴搜;

    #include <cstdio>
    int n, cnt;
    char map[5][5];
    bool Build(int row, int col){
        int i, j;
        for(int i = row; i >= 0; i--){
            if(map[i][col] == 'O')
                return false;
            if(map[i][col] == 'X')
                break;
        }
        for(int i = col; i >= 0; i--){
            if(map[row][i] == 'O')
                return false;
            if(map[row][i] == 'X')
                break;
        }
        return true ;
    } 
    void Dfs(int i, int num){
        if(i == n*n){
        //    printf("%d %d
    ", i, num);
            if(num > cnt)
                cnt = num;
            return;
        }
        else{
            int row = i / n;
            int col = i % n;
            if(map[row][col] == '.' && Build(row, col)){
                map[row][col] = 'O';
                Dfs(i+1, num+1);
                map[row][col] = '.';
            } 
            Dfs(i+1, num);
        }
    }
    int main(){
        while(scanf("%d", &n), n){
            cnt = 0;
            for(int i = 0; i < n; i++)
                scanf("%s", map[i]);
            Dfs(0, 0);
            printf("%d
    ", cnt);
        }    
        return 0;
    }
  • 相关阅读:
    JS中的继承(上)
    一篇文章理解JS继承——原型链/构造函数/组合/原型式/寄生式/寄生组合/Class extends
    JS 装饰器,一篇就够
    理解 JavaScript 的 async/await
    JS正则表达式入门,看这篇就够了
    JavaScript的几种循环方式
    全解跨域请求处理办法
    下班后的时间精力生活管理办法(时间管理)
    hexo上部署博客到Github失败
    11
  • 原文地址:https://www.cnblogs.com/soTired/p/5063031.html
Copyright © 2020-2023  润新知