zoukankan      html  css  js  c++  java
  • HDU Fire Net (二分图最大匹配+缩点建图)

    题目

    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

    思路

    这道题你发现和前面我发的那道两个车相撞的题目一样,都是二分匹配的模型,但是这里不同的是,当两点之间有一道墙的时候,我们就不会把同行或者同列的情况看作是非法的。所以我们直接做肯定是不行,这道题在建图上面巧妙的一点在于缩点,把同行同列的连通块当作是一个点来处理,这样就可以巧妙的化解题目中的条件了。

    代码实现

    #include<cstdio>
    #include<algorithm>
    #include<vector>
    #include<queue>
    #include<map>
    #include<iostream>
    #include<cstring>
    #include<cmath>
    using namespace std;
    #define rep(i,f_start,f_end) for (int i=f_start;i<=f_end;++i)
    #define per(i,n,a) for (int i=n;i>=a;i--)
    #define MT(x,i) memset(x,i,sizeof(x) )
    #define rev(i,start,end) for (int i=0;i<end;i++)
    #define inf 0x3f3f3f3f
    #define mp(x,y) make_pair(x,y)
    #define lowbit(x) (x&-x)
    #define MOD 1000000007
    #define exp 1e-8
    #define N 1000005 
    #define fi first 
    #define se second
    #define pb push_back
    typedef long long ll;
    typedef pair<int ,int> PII;
    typedef pair<int ,PII> PIII;
    ll gcd (ll a,ll b) {return b?gcd (b,a%b):a; }
    inline int read() {
        char ch=getchar(); int x=0, f=1;
        while(ch<'0'||ch>'9') {
            if(ch=='-') f = -1;
            ch=getchar();
        } 
        while('0'<=ch&&ch<='9') {
            x=x*10+ch-'0';
            ch=getchar();
        }   return x*f;
    }
    
    const int maxn=1000+5;
    int x[maxn][maxn];
    int y[maxn][maxn];
    int vis[maxn];
    int from[maxn];
    int g[maxn][maxn];
    char str[maxn][maxn];
    int n,cntx,cnty;
    
    bool find (int x) {
       rev (i,0,cnty) {
           if (!vis[i]&&g[x][i]) {
               vis[i]=1;
               if (from[i]==-1||find (from[i])) {
                   from[i]=x;
                   return true;
               }
           }
       }
       return false;
    }
    
    int hungry () {
        int ans=0;
        MT (from,-1);
        rev (i,0,cntx) {
            MT (vis,0);
            if (find (i)) ans++;
        }
        return ans;
    }
    
    int main () { 
        while (cin>>n) {
            if (n==0) break;
            MT (x,0);
            MT (g,0);
            MT (y,0);
            cntx=1,cnty=1;
    
            rev (i,0,n) cin>>str[i];
            
            rev (i,0,n) {
                rev (j,0,n) {
                    if (str[i][j]=='.') x[i][j]=cntx;
                    else  cntx++;
                }
                cntx++;
            }
            rev (j,0,n) {
                rev (i,0,n) {
                    if (str[i][j]=='.') y[i][j]=cnty;
                    else cnty++;
                }
                cnty++;
            }
    
            rev (i,0,n) 
             rev (j,0,n) {
                 if (str[i][j]=='.') {
                     int a=x[i][j];
                     int b=y[i][j];
                     g[a][b]=1;
                 }
             }
             int ans=hungry ();
             cout<<ans<<endl;
        }
        return 0; 
    }
    
  • 相关阅读:
    Discourse 如何不使用 Let’s Encrypt 而使用 CA 签名的密钥进行安装
    Discourse 重复安装过程中的密钥签发问题
    Discourse 升级后提示 https 混合内容
    CentOS 8 安装 docker 报错 containerd.io >= 1.2.2-3
    MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting transaction
    培养自己的5项能力
    高效率工作方式
    项目的架构演进过程
    如何预防后台被攻击,且看Tomcat的安全配置
    redis的缓存更新策略,缓存粒度控制
  • 原文地址:https://www.cnblogs.com/hhlya/p/13442261.html
Copyright © 2011-2022 走看看