Fire Net

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

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
 

这题用普通DFS+回溯比较好想得到,但是用二分就比较麻烦……看了下大牛的思路,是把每一个给每一个行连通分量和列连通分量标号,然后再对每一个点行列加边,可以发现每一个'.'都是一个行与列连通分量的交叉点即两者的匹配,匹配之后这个行与列连通分量中不能再出现任何的炮台,符合了匹配的思想即,然后进行建图……主要就是建图方法比较难想到……感叹想到这个建图方法的人好机智啊……

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define MM(x,y) memset(x,y,sizeof(x))
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=100;
struct info
{
int to;
int pre;
}E[N];
int head[N],cnt,vis[N],match[N];
char pos[N][N];
pii num[N][N];
void add(int s,int t)
{
E[cnt].to=t;
E[cnt].pre=head[s];
head[s]=cnt++;
}
void init()
{
MM(head,-1);
cnt=0;
MM(match,-1);
MM(pos,-1);
MM(num,0);
}
int dfs(int now)
{
for (int i=head[now]; ~i; i=E[i].pre)
{
int v=E[i].to;
if(!vis[v])
{
vis[v]=1;
if(match[v]==-1||dfs(match[v]))
{
match[v]=now;
match[now]=v;
return 1;
}
}
}
return 0;
}
int main(void)
{
int n,i,j,k,ur,ul;
while (~scanf("%d",&n)&&n)
{
init();
char c;
for (i=0; i<n; i++)
scanf("%s",pos[i]);
ur=ul=0;
for (i=0; i<n; i++)
{
int flag=1;
for (j=0; j<n; j++)
{
if(pos[i][j]!='X')
{
if(flag)
{
num[i][j].first=++ur;
flag=0;
}
else
num[i][j].first=num[i][j-1].first;
}
else
flag=1;
}
}
for (j=0; j<n; j++)
{
int flag=1;
for (i=0; i<n; i++)
{
if(pos[i][j]!='X')
{
if(flag)
{
num[i][j].second=++ul;
flag=0;
}
else
num[i][j].second=num[i-1][j].second;
}
else
flag=1;
}
}
for (i=0; i<n; i++)
{
for (j=0; j<n; j++)
{
if(num[i][j].first&&num[i][j].second)
add(num[i][j].first,ur+ul+num[i][j].second);
}
}
int r=0;
for (i=1; i<=ur; i++)
{
MM(vis,0);
if(dfs(i))
r++;
}
printf("%d\n",r);
}
return 0;
}

HDU——1045Fire Net(最大匹配)的更多相关文章

  1. HDU 2853 (KM最大匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2853 题目大意:二分图匹配费用流.①最大匹配②最小原配变动 解题思路: 如果去掉第二个要求,那么就是裸 ...

  2. hdu 1281 二分图最大匹配

    对N个可以放棋子的点(X1,Y1),(x2,Y2)......(Xn,Yn);我们把它竖着排看看~(当然X1可以对多个点~) X1   Y1 X2   Y2 X3   Y3 ..... Xn   Yn ...

  3. hdu 4619 二分图最大匹配 ——最大独立集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4619 #include <cstdio> #include <cmath> # ...

  4. B - The Accomodation of Students - hdu 2444(最大匹配)

    题意:现在有一些学生给你一下朋友关系(不遵守朋友的朋友也是朋友),先确认能不能把这些人分成两组(组内的人要相互不认识),不能分的话输出No(小写的‘o’ - -,写成了大写的WA一次),能分的话,在求 ...

  5. HDU 3279 二分图最大匹配

    DES: 就是说对每个人都给你一个区间.但一个人只匹配一个数.问你满足匹配的人的序号字典序最大时的最大匹配是什么. 前几天刚做的UVALive 6322...当然是不一样的...那个要求的最大匹配的个 ...

  6. HDU - 2444 二分图最大匹配 之 判断二分图+匈牙利算法

    题意:第一行给出数字n个学生,m条关系,关系表示a与b认识,判断给定数据是否可以构成二分图,如果可以,要两个互相认识的人住一个房间,问最大匹配数(也就是房间需要的最小数量) 思路:要看是否可以构成二分 ...

  7. hdu 3729(二分图最大匹配)

    I'm Telling the Truth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  8. hdu 4185 二分图最大匹配

    Oil Skimming Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  9. hdu 1083 Courses (最大匹配)

    CoursesTime Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

随机推荐

  1. Android 视频录制 java.lang.RuntimeException: start failed.

    //mRecorder.setVideoSize(320, 280); // mRecorder.setVideoFrameRate(5); mRecorder.setOutputFile(viodF ...

  2. Android线程池(转)

    .前言 转载请注明出处:http://blog.csdn.net/seu_calvin/article/details/52415337 使用线程池可以给我们带来很多好处,首先通过线程池中线程的重用, ...

  3. rxjava封装,RxBus封装(上线项目集成,声明周期管理,处理溢出内存,支持同时多个请求。)

    Github地址 RxLibrary工程:1.rxjava2 + retrofit2的封装,常用的请求(Get,Post,文件上传,文件下载),防止内存泄漏,简单便捷,支持自定义loading等属性. ...

  4. hdu 6058 Kanade's sum (计算贡献,思维)

    题意: 给你一个全排列,要你求这个序列的所有区间的第k大的和 思路:比赛的时候一看就知道肯定是算贡献,也知道是枚举每个数,然后看他在多少个区间是第K大,然后计算他的贡献就可以了,但是没有找到如何在o( ...

  5. C# Process.Start方法

    System.Diagnostics.Process.Start(); 主要功能: 1.打开某个链接网址(弹窗). 2.定位打开某个文件目录. 3.打开系统特殊文件夹,如“控制面板”等. Proces ...

  6. Mac终端(Terminal)自定义颜色,字体,背景

    使用Mac作为开发机的时候,苹果终端自带的颜色黑白,字体又小,看起来确实不是很舒服.那推荐大家使用Solarized配色方案.Solarized 是目前最完整的 Terminal/Editor/IDE ...

  7. javase(13)_网络编程

    一.概述 1.网络编程的核心是IP.端口(表示应用程序).协议三大元素 2.网络编程的本质是进程间通信 3.网络编程的2个主要问题:1是定位主机,2是数据传输 二.网络通信的概念 1.网络通信协议 计 ...

  8. Linux关于FTP安全

    https://www.cnblogs.com/Hyber/archive/2017/02/04/6362916.htmlhttps://www.cnblogs.com/ichunqiu/p/7300 ...

  9. CONTEST1001 题解

    PROBLEM A 分析 这个题属于非常基础的输出问题,一般来说见到这种题可以直接复制粘贴即可. 讲解 没有什么详细说明的直接复制粘贴即可.这样不容易出错. 代码 #include <stdio ...

  10. UVa-213-信息解码

    这题的话,我们只要理解题意,应该就不算很难. 我们可以开一个二维数组,用来存放对应的编码字符,第一个下表是length,第二个下标是value,这样一来,我们在读入数据的时候就进行处理,然后想要使用的 ...