HDU 1045 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

题意:有n条n列的街道,街道上‘.‘表示空地,‘X‘表示障碍物,现在要求在街道上建立房子,但是每行每列如果没有障碍物,则只能有一间房子,如果有障碍物阻拦则可能不止可以建一座房子,问最多可以建几座房子

1.二分匹配:

定义一段无法向左右扩展的连续非障碍物方格为行连通块,一段无法向上下扩展的连续非障碍物方格为列连通块
我们把行连通块对应X集合,列连通块对应Y集合,如果一个行连通块与一个列连通块有交点且为空地,则对应在二分图中有一条边
显然房子的集合对应二分图的一个匹配,任意两个房子不可能共同存在于一个行或列连通块中,所以最多房子数目=最大匹配数
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N 10
using namespace std;
char Map[N][N];
int G[N][N];
int use[N], vis[N];
int n, rn, cn;
int Find(int u)
{
    int i;
    for (i = 1; i <= cn; i++)
    {
        if (!vis[i] && G[u][i])
        {
            vis[i] = 1;
            if (!use[i] || Find(use[i]))
            {
                use[i] = u;
                return 1;
            }
        }
    }
    return 0;
}
int main ()
{
    int i, j, r, c, ans;
    int mapr[N][N], mapc[N][N];
    while (scanf("%d", &n), n)
    {
        r = c = rn = cn = ans = 0;
        memset(G, 0, sizeof(G));
        memset(use, 0, sizeof(use));
        memset(mapr, 0, sizeof(mapr));
        memset(mapc, 0, sizeof(mapc));
        for (i = 0; i < n; i++)
            scanf("%s", Map[i]);
        for (i = 0; i < n; i++)
            for (j = 0; j < n; j++)
                if (Map[i][j] == ‘X‘)
                    mapr[i][j] = mapc[i][j] = -1;
        for (i = 0; i < n; i++)
        {
            for (j = 0; j < n; j++)
            {
                while (mapr[i][j] == -1 && j < n)
                    j++;
                r++;
                while (mapr[i][j] != -1 && j < n)
                {
                    mapr[i][j] = r;            //建立行连通块
                    if (rn < r) rn = r;
                    j++;
                }
            }
        }
        for (j = 0; j < n; j++)
        {
            for (i = 0; i < n; i++)
            {
                while (mapc[i][j] == -1 && i < n)
                    i++;
                c++;
                while (mapc[i][j] != -1 && i < n)
                {
                    mapc[i][j] = c;           //建立列连通块
                    if (cn < c) cn = c;
                    i++;
                }
            }
        }
        for (i = 0; i < n; i++)
            for (j = 0; j < n; j++)
                if (mapr[i][j] != -1 && mapc[i][j] != -1)
                   G[mapr[i][j]][mapc[i][j]] = 1; //重新构图
        for (i = 1; i <= rn; i++)
        {
            memset(vis, 0, sizeof(vis));
            if (Find(i)) ans++;
        }
        printf("%d\n", ans);
    }
    return 0;
}

2.该题还可以暴力搜索:

#include<stdio.h>
#include<algorithm>
#define N 10
using namespace std;
char Map[N][N];
int n, Max;
int Judge(int x, int y) //判断该行该列是否已经建过房子或者有障碍物
{
    int i;
    for (i = x-1; i >= 0; i--)
    {
        if (Map[i][y] == ‘#‘) //建过房子,则不能再建
            return 0;
        if (Map[i][y] == ‘X‘) //有障碍物,则还能再建
            break;
    }
    for (i = y-1; i >= 0; i--)
    {
        if (Map[x][i] == ‘#‘)
            return 0;
        if (Map[x][i] == ‘X‘)
            break;
    }
    return 1;
}
void DFS(int s, int c) //s表示走的步数,c表示可以建几座房子
{
    int x, y;
    Max = max(Max, c);
    if (s == n*n) return ;
    x = s / n; //x表示行
    y = s % n; //y表示列
    if (Map[x][y] == ‘.‘ && Judge(x, y))
    {
        Map[x][y] = ‘#‘;
        DFS(s+1, c+1); //如果这点可以建立房子,则步数增加,房子总数增加
        Map[x][y] = ‘.‘;
    }
    DFS(s+1, c); //即使这个点不能建房子,步数依然要增加
}
int main ()
{
    int i;
    while (scanf("%d", &n), n)
    {
        for (i = 0; i < n; i++)
            scanf("%s", Map[i]);
        Max = 0;
        DFS(0, 0);
        printf("%d\n", Max);
    }
    return 0;
}
时间: 2024-10-09 14:03:45

HDU 1045 Fire Net(缩点求最大匹配)的相关文章

HDU 1045 - Fire Net (最大独立集)

题意:给你一个正方形棋盘.每个棋子可以直线攻击,除非隔着石头.现在要求所有棋子都不互相攻击,问最多可以放多少个棋子. 这个题可以用搜索来做.每个棋子考虑放与不放两种情况,然后再判断是否能互相攻击来剪枝.最后取可以放置的最大值. 这里我转化成求最大独立集来做. 首先将每个空地编号,对于每个空地,与该位置可以攻击到的空地连边.找最多的空地使得不互相攻击,即求该图的最大独立集.与搜索做法基本一致,但是说法略有不同. 1 #include<iostream> 2 #include<cstring

HDU 1045 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 wh

HDU 1045——Fire Net——————【最大匹配、构图、邻接矩阵做法】

Fire Net Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1045 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, e

hdu 1045 Fire Net(最小覆盖点+构图(缩点))

http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1045 Description Suppose that we have a square city with straight streets. A map of a city

HDU 1045 Fire Net 状压暴力

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8073    Accepted Submission(s): 4626 Problem Description Suppose that we have a squar

HDU 1045 Fire Net(dfs,跟8皇后问题很相似)

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 14670    Accepted Submission(s): 8861 Problem Description Suppose that we have a squar

HDU - 1045 Fire Net (二分图最大匹配-匈牙利算法)

(点击此处查看原题) 匈牙利算法简介 个人认为这个算法是一种贪心+暴力的算法,对于二分图的两部X和Y,记x为X部一点,y为Y部一点,我们枚举X的每个点x,如果Y部存在匹配的点y并且y没有被其他的x匹配,那就直接匹配:如果Y中已经没有可以和x匹配的点(包括可以匹配的点已经被其他的x匹配),那就让已经匹配的y的原配x'寻找其他可以匹配的y’,并将y和x匹配,最后,统计出匹配的对数 (详细了解的话,可以看看这位的博客:https://blog.csdn.net/sunny_hun/article/de

HDU 1045 Fire Net 二分图Bipartite题解

本题可以使用DFS直接爆搜出答案,不过这样类型的题目其实是个二分图的题解. 这个二分图,难不在Hungary算法,而是难在于建图.需要挺高的抽象思维的. 建图: 1 把同一行不被X分开的格子标同一个号码,被X分开的标下一个号码,这样做是为了缩点,不需要把所有的格子都分开标号,而且可以更方便建个更加小的图. 2 同理把同一列的格子标号 3 然后判断相同一个格子的行标号和列标号是有路径的,其他不在同一个格子的都是没有路径的. 4 这样就等于以行标号和列标号作为左右顶点,构建成一个二分图了 然后使用H

HDU - 1045 Fire Net (dfs 或 二分图)

题意:给出一个不会超过4x4的mapmap中有墙,以及空白处.然后你要在空白处放置尽可能多的炮台炮台对向四周发射子弹,即(炮台不能放在同一行或者列除非有强阻挡)思路:首先想到了dfs枚举(就像八皇后一样回溯法),我们尽可能多的在一行一行的放置.关于放置搜索的问题,我们判断是否合法关于二分图匹配(完全没有想出来怎么匹配..)在网上看了别人的题解才懂由于同行或者同列不能多个放置(即每行每列只能放一个除了有墙挡)然后就是建图:将每行每列连续的部分缩成一个点如图:(画的真的丑...) 我们然后这就有连接