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. 【转载】Fiddler抓包及模拟服务端

    此文章转载公众号‘云测学院'链接:https://mp.weixin.qq.com/s/qXmBDh980nBJ8IchbRGC3Q 及公众号gloryroadtrain 在HTTP接口的测试过程中, ...

  2. Python学习day34-面向对象和网络编程总结

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  3. java扫描某个包下的所有java类并加载

    最近在学习java的反射和注解,实际情景中需要扫描某个包下的所有java类,然后使用类加载器加载类. 基本思路,获得程序的路径扫描src下某个包内的子包和java类,实现也比较简单. 运行环境:win ...

  4. python 描述器

    语法简析 一般来说,描述器(descriptor)是一个有”绑定行为”的对象属性(object attribute),它的属性访问被描述器协议方法重写.这些方法是 __get__(). __set__ ...

  5. STL与泛型编程-练习2-GeekBand

    练习题目: struct Programmer{ Programmer(const int id, const std::wstring name): Id(id), Name(name){ } vo ...

  6. grant

    # 添加超级用户 grant all privileges on *.* to 'dump_tmp'@'10.10.10.10' identified by 'dump_tmp'; grant all ...

  7. js的相关距离

    js的相关距离 一.dom对象的距离 ---dom.style.width : 对象本身的内容宽度(这里必须是内联样式中的width,带px)(content) ---dom.style.height ...

  8. 转:PLL 锁相环

    原地址:http://fangjian0518.blog.163.com/blog/static/559196562011210103455430/  PLL的作用? 答:LPC2000系列ARM内部 ...

  9. https://github.com/ronggang/transmission-web-control

    首先需要保证你的电脑可以链接国际互联网 如果只能链接大型局域网,那么请您带着电脑出国以便于可以顺利安装https://github.com/ronggang/transmission-web-cont ...

  10. HTML5:使用Canvas和Input range控件放大缩小图片,剪裁,并上传图片

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...