首页 技术 正文
技术 2022年11月22日
0 收藏 907 点赞 2,642 浏览 3504 个字

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 DescriptionSuppose 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 4×4) 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.

HDU——1045Fire Net(最大匹配)

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.  InputThe 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.  OutputFor each test case, output one line containing the maximum number of blockhouses that can be placed in the city in a legal configuration. Sample Input4
.X..
….
XX..
….
2
XX
.X
3
.X.
X.X
.X.
3

.XX
.XX
4
….
….
….
….
0 Sample Output5
1
5
2

这题用普通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;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,028
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,518
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,365
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,146
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,780
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,857