以防万一,题目原文和链接均附在文末。那么先是题目分析:

【一句话题意】

给定大小的棋盘中部分格子存在可以阻止互相攻击的墙,问棋盘中可以放置最多多少个可以横纵攻击炮塔。

【题目分析】

这题本来在搜索专题里出现的..这回又在二分查找匹配专题出现了..所以当然要按照二分匹配的方法解而不是爆搜(虽然爆搜能过)。
问题主要就是如何缩点建图。为了使得blockhouse不能互相攻击,那么使用每行的相邻的点缩点,每列的相邻的点缩点,连边的条件就是两个点存在有相交的部分,最后这两组点求最大匹配就行了。

【算法流程】

应该这题也能算是标准题了吧。缩点,建图,hungary匈牙利算法求解,输出答案。完事儿了。
匈牙利算法以及二分最大匹配的相关内容这里就不说了。。

下面的代码简单说明,本来的aleft和aright名字叫left和right,后来发现和STL的东西冲突了。。
fill的宏定义有更科学的方法,这里就只是单纯的抹0。以及for each是从1到n而不是从0到n。
因为习惯问题,数组是从1算起的。

 #include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
#include <vector>
#define each(i,n) (int i=1;i<=(n);++i)
#define fill(arr) memset(arr,0,sizeof(arr))
#define INF 0x3f3f3f3f char map[][];
char aleft[][];
char aright[][];
int l[], r[], lCnt, rCnt;
bool graph[][];
bool vis[]; using namespace std; int dfs(int left) {
for each(right,rCnt) {
//left到right有路且没遍历过
if(graph[left][right] && !vis[right]) {
vis[right] = true;
//若right还没匹配过或跟right匹配的点找到另一个相匹配的点
//(则right就可以跟left匹配)
if(r[right]==- || dfs(r[right])) {
r[right] = left;
l[left] = right;
//printf("(%d,%d)\n",left,right);
return ;
}
}
}
return ;
} int hungary() {
int ans = ;
memset(l,-,sizeof(l));
memset(r,-,sizeof(r));
for each(i,lCnt) { //row point cnt
if(l[i] == -) {
fill(vis);
ans += dfs(i);
}
}
return ans;
} int main() { int n;
char buffer[];
while(gets(buffer)) {
//proc input
if (buffer[] == '') break;
sscanf(buffer,"%d",&n);
for each(line,n) {
gets(buffer);
for each(col,n) {
map[line][col] = buffer[col-];
}
}
//create grapth [ Marking up Points ]
fill(aleft);
fill(aright);
lCnt = ;
int pre = ;
for each(row,n) {
for each(col,n) {
if(map[row][col]=='X') {
if(pre==lCnt) ++lCnt;
continue;
}
aleft[row][col] = lCnt;
pre = lCnt;
}
if(pre==lCnt) ++lCnt;
}
lCnt = pre;
rCnt = ;
pre = ;
for each(col,n) {
for each(row,n) {
if(map[row][col]=='X') {
if(pre==rCnt) ++rCnt;
continue;
}
aright[row][col] = rCnt;
pre = rCnt;
}
if(pre==rCnt) ++rCnt;
}
rCnt = pre;
//create grapth [ Shrinking Points ]
fill(graph);
for each(row,n) {
for each(col,n) {
if(map[row][col]=='.')
graph[aleft[row][col]][aright[row][col]] = true;
}
}
//doWork
printf("%d\n",hungary());
} }

题目链接:Fire Net(HDU 1045)

题目属性:二分图最大匹配 (如果愿意你可以去爆搜..)

相关题目:2444 1083 1281 2819 2389 4185 poj3020 ...

题目原文:
【desc】
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.
【In】
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.
【Out】
For each test case, output one line containing the maximum number of
blockhouses that can be placed in the city in a legal configuration.

【SampIn】
4
.X..
....
XX..
....
2
XX
.X
3
.X.
X.X
.X.
3
...
.XX
.XX
4
....
....
....
....
0
【SampOut】
5
1
5
2
4

HDU 1045(Fire Net)题解的更多相关文章

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

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

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

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

  3. HDU 1045 Fire Net 状压暴力

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

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

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

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

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

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

    Fire Net Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  7. HDU 1045 Fire Net 二分图建图

    HDU 1045 题意: 在一个n*n地图中,有许多可以挡住子弹的墙,问最多可以放几个炮台,使得炮台不会相互损害.炮台会向四面发射子弹. 思路: 把行列分开做,先处理行,把同一行中相互联通的点缩成一个 ...

  8. HDU 1045 - Fire Net - [DFS][二分图最大匹配][匈牙利算法模板][最大流求二分图最大匹配]

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

  9. hdu 1045 Fire Net(二分图)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1045 题目大意为给定一个最大为4*4的棋盘,棋盘可以放置堡垒,处在同一行或者同一列的堡垒可以相互攻击, ...

随机推荐

  1. [Math]Sqrt(x)

    Total Accepted: 75767 Total Submissions: 314003 Difficulty: Medium Implement int sqrt(int x). Comput ...

  2. oracle DBLink

    现有两个oracle DB为A和B,为了能在A数据库中对B数据库进行操作,我们需要在A数据库中建立对B的DBLink.    在创建DBLink之前,我们首先去检查下DB A的global_names ...

  3. php-fpm日志告警"seem busy"

    php-fpm日志中出现"WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.m ...

  4. 创建一个Hello World模块

    这篇文章描述了怎样为Orchard建立一个非常小的模块,它只显示一个"Hello World"页面. 另一个模块[Module]的简单例子,你可以从这找到:Quick Start ...

  5. 如何设置 Internal 类,方法,属性对其他项目可见

    internal 修饰符时程序集内部其他类可见.但对程序集外的类不可见,这样就很好的封装了可见性. 但是,我们也许会碰到类似的情况,特别是基于已有代码开发新的代码时,你会发现很多 internal 的 ...

  6. QP在STM32F10X上第一个应用

        两天没有写博客了,这两天主要还是在考虑软件的结构性问题,用不用QP?用不用ST库函数?看了ucos,freertos,tinyos以及Contiki,库函数的问题看了使用库的软件结构,直接操作 ...

  7. Linux进程间通信——使用命名管道

    在前一篇文章——Linux进程间通信——使用匿名管道中,我们看到了如何使用匿名管道来在进程之间传递数据,同时也看到了这个方式的一个缺陷,就是这些进程都由一个共同的祖先进程启动,这给我们在不相关的的进程 ...

  8. 如何让FPGA中的SPI与其他模块互动起来

    在上一篇文章<FPGA的SPI从机模块实现>中,已经实现了SPI的从机模块,如何通过SPI总线与FPGA内部其他模块进行通信,是本文的主要讨论内容. 一. 新建FPGA内部DAC控制模块 ...

  9. MVC与WebForm最大的区别

    原文地址:http://www.cnblogs.com/birdshover/archive/2009/08/24/1552614.html 使用ASP.NET MVC框架,创建默认项目,第一直观感觉 ...

  10. cp | mv | rm

    linux下文件的复制.移动与删除命令为:cp,mv,rm 一.文件复制命令cp 命令格式: cp [-adfilprsu] 源文件(source) 目标文件(destination) cp [opt ...