zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    解决:导出excel身份证号码显示为科学计数法
    6个出色的基于JQuery的Tab选项卡实例2010/01/29 16:261. jQuery 选项卡界面 / 选项卡结构菜单教程
    dhl:禁用firefox缓存
    artDialog4.0.5
    dhl:解除ASP.NET上传文件大小限制
    dhl:样式在ie不同浏览器下呈现不出来的原因分析
    dhl:artDialog 3.0.4 跨框架下 穿越的问题
    jQuery 1.7 正式版已经可以下载使用。jQuery是一个JavaScript库,它简化了HTML文档遍历,事件处理,动画和为网络快速发展的Ajax交互。jQuery 1.7 版本加入了新的事件API .on() 和 .off(),提
    无法向会话状态服务器发出会话状态请求。请确保 ASP.NET State Service (ASP.NET 状态服务)已启动,并且客户端端口与服务器端口相同。
    专家视角看IT与架构
  • 原文地址:https://www.cnblogs.com/soTired/p/5063031.html
Copyright © 2011-2022 走看看