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. 《DSP using MATLAB》Problem 8.19

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  2. CodeForces - 1087D

    CodeForces - 1087Dhttps://vjudge.net/problem/2115151/origin2*和/叶子结点的个数 #include<iostream> #inc ...

  3. AtCoder - 3959

    AtCoder - 3959https://vjudge.net/problem/1583855/origin求最长连续递增长度就行,答案是n-max(len) #include<iostrea ...

  4. HZOI20190903模拟36 字符,蛋糕,游戏

    题面:https://www.cnblogs.com/Juve/articles/11461528.html A:字符 暴力模拟一下,细节很多,但是暴力思路都不大一样 先枚举循环节长度,然后处理一个b ...

  5. win10文件名或文件路径过长导致无法删除或复制的解决办法

    试过了百度上的所有方法,命令行中del没有作用,Unlocker也没用,批处理也不起作用,360的强力删除也没有作用. 最后找到一种方法,在压缩该文件的时候选择删除源文件. 但是需要注意一点,用360 ...

  6. 2019.10.28 csp-s模拟测试91 反思总结

    有一场没一场的233 T1: 胡乱分析一下题意,发现和为n的x个正整数,不同的数字种类不会超过√n个.假设这x个数字都不同,最多也就是(x+1)*x/2=n. 所以可以维护现有的size值以及对应的数 ...

  7. ssm整合:搭建环境

    解决配置中文过滤器后,存入数据库时依旧乱码问题:在web.xml中修改数据库url如下: <property name="jdbcUrl" value="jdbc: ...

  8. Luogu P2486 [SDOI2011]染色(树链剖分+线段树合并)

    Luogu P2486 [SDOI2011]染色 题面 题目描述 输入输出格式 输入格式: 输出格式: 对于每个询问操作,输出一行答案. 输入输出样例 输入样例: 6 5 2 2 1 2 1 1 1 ...

  9. 支付宝单笔转账demo (改配置直接用)

    支付宝接口说明文档  https://docs.open.alipay.com/api_28/alipay.fund.trans.toaccount.transfer 需要注意的是:rsa的  公钥和 ...

  10. Django项目:CRM(客户关系管理系统)--55--46PerfectCRM实现登陆后页面才能访问

    #urls.py """PerfectCRM URL Configuration The `urlpatterns` list routes URLs to views. ...