hdu 1045 二分匹配練習 重在轉化啊!!

Fire Net

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


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
 
忍不住再水一道     這是我第一次完全是自己轉化出來的二分匹配題    先說好代碼醜 臭 長    想看簡介的還是點下標籤的  叉叉  吧
但畢竟是自己的親兒子   蛤蛤

因爲這題的矩陣範圍是4*4   特別小   暴搜也是隨便過的    這裏我就不貼了   和之前寫的一道超暴力回溯差不多    但是簡單的很多

還是講講二分的吧   如果是平時一對一的匹配   只要  把行看成一個集合  把列看成一個集合進行匹配即可    但這裏一行可以與多列匹配   一列與多行匹配  需要重新建圖

我的爛思路就是再開兩個矩陣    一個記錄新的  行   一個記錄新的列   再建立方向矩陣   套模板即可

ac code

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;
int n,nrow,ncol;
const int maxn=16;
bool g[maxn][maxn],vis[maxn];
char mat[maxn][maxn];
int row[maxn][maxn],col[maxn][maxn],link[maxn];
int hungary();
int dfs(int x);

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0) break;
        getchar();
        for(int i=1; i<=n; i++) scanf("%s",mat[i]+1);
        int num=1,flag[3];
        for(int i=1; i<=n; i++)//建立行矩陣
        {
            flag[0]=flag[1]=flag[2]=0;
            for(int j=1; j<=n; j++)
                if(mat[i][j]=='X')
                {
                    row[i][j]=0;
                    flag[1]=j;
                }
                else
                {
                    if(flag[0]&&flag[1]&&flag[0]<flag[1])//如果同時存在.和X  並且  .  在X 前面
                    {
                        row[i][j]=n+num;
                        flag[2]=1;
                    }
                    else row[i][j]=i;
                    if(!flag[0]) flag[0]=j;
                }
            if(flag[2]) num++;
        }
        nrow=n+num-1;//新行長度

        num=1;
        for(int i=1; i<=n; i++)//建立新列 與上相同
        {
            flag[0]=flag[1]=flag[2]=0;
            for(int j=1; j<=n; j++)
                if(mat[j][i]=='X')
                {
                    col[j][i]=0;
                    flag[1]=j;
                }
                else
                {
                    if(flag[0]&&flag[1]&&flag[0]<flag[1])
                    {
                        col[j][i]=n+num;
                        flag[2]=1;
                    }
                    else col[j][i]=i;
                    if(!flag[0]) flag[0]=j;
                }
            if(flag[2]) num++;
        }
        ncol=n+num-1;

        memset(g,false,sizeof g);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                g[row[i][j]][col[i][j]]=true;//建立鄰接矩陣
        g[0][0]=false;
        int ans=hungary();//套模板
        printf("%d\n",ans);
    }
    return 0;
}

int hungary()
{
    int num=0;
    memset(link,-1,sizeof link);
    for(int i=1;i<=nrow;i++)
    {
        memset(vis,false,sizeof vis);
        num+=dfs(i);
    }
    return num;
}

int dfs(int x)
{
    for(int i=1;i<=ncol;i++)
        if(!vis[i]&&g[x][i])
        {
            vis[i]=true;
            if(link[i]==-1||dfs(link[i]))
            {
                link[i]=x;
                return 1;
            }
        }
    return 0;
}


學長總是說   圖論只要套模板就好了   還有個關鍵就是你要套的來阿……

所以   以我現在的水平判斷    轉化建圖再圖論裏是比較重要的          有些題    你根本看不出來是圖論題    但是一轉化  就會豁然開朗

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章