Fire Net

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

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
 

渣渣一枚只会暴力搜索,查阅了很多资料以后,也就是找到了一个二分匹配的最简写法。

1.匹配算法(匈牙利算)

题意:给出一个n*n的矩阵,其中‘.’表示空地,‘X’表示城墙,空地上可以放炮台,城墙可以阻挡炮台,任意两个炮台不能照脸。求最多能放多少个炮台。

思路:按照行和列一一对应可以构建一个二分图,最大匹配数就是结果。

比较难想的是怎么建图,对于下图,如果(1,1)和(1,3)这两个点是可以同时放炮台的。这种情况相当于将第一行分成了两行,所以(1,3)应该与 (1,1)区分出来独立建图。开始我的想法是记录该点之前遇到过多少城墙,根据遇到的城墙数+当前行列数+n建图,这样建图相当于对原来图中的点进行了平 移,但是这样平移有一个问题。举个例子,就是2+3+n和3+2+n会在同一行。这个例子不一定准确,只是说明在平移后本来一部分在原图中可以同时成立的 匹配在新图中因为没有X反倒不能同时存在了。

可以举一种简单的例子来否定这种情况,新图相当于对原图所有的X进行处理,新图中没有X,但是图的长和宽都扩大了一倍。比如说对于5*5的图(题目规定最 大值为4,这里只是举例),如果按照上面的建图方法,长和宽同时增大一倍。那么新图中最多能同时存在十个点。按照‘.’和‘X’逐个隔开的方式画图,可以 看出这种方法明显是错的。

后来想了一下,对于没有遇到城墙的点应该保留在原图,遇到一次城墙的点应该在下一个图里。对于城墙后面的点的位置不是调整到原图之后,而是完全存在一个新的图里面。所以我修改了一下建图的方法,改为当前行列数+遇到城墙数*n,修改之后提交AC。

因为对于一个n*n的图,原图的长和边的长度最多增加(n-1)*n,所以新图的点数最多应该是(n-1)*n+n,即n*n。

.X..

....

XX..

....

 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <climits>
#include <queue>
#define ll long long using namespace std; const int N = ;
int head[N],total,visit[N];
int link[N],flag_x[N],flag_y[N];
char maze[][]; struct nodes
{
int e,next;
}Edge[N]; void add(int x,int y)
{
Edge[total].e = y;
Edge[total].next = head[x];
head[x] = total++;
} int dfs(int f)
{
int u = head[f];
for(int i = u; i != -; i = Edge[i].next)
{
int s = Edge[i].e;
if(visit[s]) continue;
visit[s] = ;
if(link[s] == - || dfs(link[s]))
{
link[s] = f;
return ;
}
}
return ;
} void init()
{
total = ;
memset(head,-,sizeof(head));
memset(flag_x,,sizeof(flag_x));
memset(flag_y,,sizeof(flag_y));
memset(link,-,sizeof(link));
}
int main(void)
{
int n,i,j,cnt;
while(scanf("%d",&n),n)
{
init();
for(i = ; i <= n; i++)
scanf("%s",maze[i]+); for(i = ; i <= n; i++)
{
for(j = ; j <= n ; j++)
{
if(maze[i][j] == '.')
add(n*flag_x[i]+i,n*flag_y[j]+j);
else
flag_x[i]++,flag_y[j]++;//讲城墙隔开的点建到新图里
}
}
for(cnt = ,i = ; i <= n * n; i++)
{
memset(visit,,sizeof(visit));
if(dfs(i))
cnt++;
}
printf("%d\n",cnt);
}
return ;
}

2.暴力深搜

 #include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#include <cmath>
#define PI acos(-1.0)
using namespace std;
char maps[][];
int visit[][],n,ans,k;
struct nodes
{
int x,y;
} use[];
void dfs(int th,int cur)
{
if(th >= k)
return;
dfs(th+,cur);
int x = use[th].x;
int y = use[th].y;
for(int i = y-; i >= ; i--)
{
if(maps[x][i] == 'X')
break;
if(visit[x][i] == )
{
return;
}
}
for(int i = x-; i >= ; i--)
{
if(maps[i][y] == 'X')
break;
if(visit[i][y] == )
{
return;
}
} visit[x][y] = ;
ans = max(ans,cur+);
dfs(th+,cur+);
visit[x][y] = ;
}
int main(void)
{
while(scanf("%d",&n) ,n)
{
ans = ;
k = ;
memset(visit,,sizeof(visit));
for(int i = ; i <n; i++)
scanf("%s",maps[i]);
for(int i = ; i < n; i++)
for(int j = ; j < n; j++)
{
if(maps[i][j] == '.')
use[k].x = i,use[k++].y = j;
}
dfs(,);
printf("%d\n",ans);
}
return ;
}

hdu 1045 Fire Net(二分匹配 or 暴搜)的更多相关文章

  1. hdu 1045 Fire Net 二分图匹配 && HDU-1281-棋盘游戏

    题意:任意两个个'车'不能出现在同一行或同一列,当然如果他们中间有墙的话那就没有什么事,问最多能放多少个'车' 代码+注释: 1 //二分图最大匹配问题 2 //难点在建图方面,如果这个图里面一道墙也 ...

  2. HDU 1045 Fire Net(行列匹配变形+缩点建图)

    题意:n*n的棋盘上放置房子.同一方同一列不能有两个,除非他们之间被墙隔开,这种话. 把原始图分别按行和列缩点 建图:横竖分区.先看每一列.同一列相连的空地同一时候看成一个点,显然这种区域不可以同一时 ...

  3. HDOJ(HDU).1045 Fire Net (DFS)

    HDOJ(HDU).1045 Fire Net [从零开始DFS(7)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HD ...

  4. HDU - 1045 Fire Net(二分匹配)

    Description Suppose that we have a square city with straight streets. A map of a city is a square bo ...

  5. HDU 1045 Fire Net 【连通块的压缩 二分图匹配】

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others)    ...

  6. HDU 1045 Fire Net 【二分图匹配】

    <题目链接> 题目大意: 这题意思是给出一张图,图中'X'表示wall,'.'表示空地,可以放置炮台,同一条直线上只能有一个炮台,除非有'X'隔开,问在给出的图中最多能放置多少个炮台. 解 ...

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

    http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit:1000MS     Memory Limit:32768KB   ...

  8. HDU 1045(Fire Net)题解

    以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定大小的棋盘中部分格子存在可以阻止互相攻击的墙,问棋盘中可以放置最多多少个可以横纵攻击炮塔. [题目分析] 这题本来在搜索专题 ...

  9. hdu 1045:Fire Net(DFS经典题)

    Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

随机推荐

  1. centos7 yum 安装tomcat7

    查看yum中tomcat信息 yum info tomcat 安装 yum install tomcat 安装管理界面 yum install tomcat-webapps tomcat-admin- ...

  2. oracle中utl_file包读写文件操作实例学习

    在oracle中utl_file包提供了一些操作文本文件的函数和过程,学习了一下他的基本操作 1.创建directory,并给用户授权 复制代码 代码如下: --创建directory create ...

  3. 备份和恢复MySQL数据库

    一.备份 1) 备份表mysqldump -uroot -p 库名 表1 > e:\backup.sqlmysqldump -uroot -p 库名 表1 表2 表3 > e:\backu ...

  4. 内容溢出文字用"..."代替 以及超出文本内容换行

    text-overflow:ellipsis; white-space:nowrap; overflow:hidden; 超出.....代替 overflow: hidden; word-break: ...

  5. Python,anaconda及pycharm安装过程笔记

    1.Python Python有2.X和3.X版本,可以在Windows系统下共存.方法为:Windows下Python多版本共存 可参考: Python及pycharm安装 安装Python后可在c ...

  6. Java虚拟机系列(五)---垃圾收集(GC)

    Java语言最大的优势除了它的平台无关性之外,还有它的自动内存分配和垃圾收集技术,本节我先来总结一下垃圾收集相关的内容.本文将从解答以下三个问题的角度来展开: 1.哪些内存需要回收? 2.什么时候回收 ...

  7. pycharm 汉化补丁

    找了很久,找到了这个比较好的pycharm汉化补丁包,就分享出来 1. 将解压包解压出来 2 . 复制对应的汉化补丁包到 x:\xxx\JetBrains\PyCharm Community Edit ...

  8. Webpack下创建vue项目-非vue-cli

    开始准备 初始化工程目录 shell npm init -y 安装vue npm install vue 安装 webpack npm install webpack --save-dev webpa ...

  9. Eureka 客户端连接Eureka服务端时 报Cannot execute request on any known server 解决办法

    报Cannot execute request on any known server 这个错,总的来说就是连接Eureka服务端地址不对. 因为配置eureka.client.serviceUrl. ...

  10. docker和宿主机之间复制文件

    从主机复制到容器sudo docker cp host_path containerID:container_path 从容器复制到主机sudo docker cp containerID:conta ...