Fire Net

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
 
Meaning
在n*n的地图上防止尽可能多的blockhouse(碉堡),条件是碉堡不能看见另一个碉堡,然后地图上是有Fire Net的,可以隔开两个碉堡。
 
Answer
完全暴力DFS,就是写那个check函数有点烦。和经典的dfs不同之处是他的参数不是x和y,而是depth(深度)。每次搜索完成后(找不到能放置碉堡的地方),比较深度和ans的大小。
 
#include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
char mp[][];
int n,ans;
bool check(int x,int y)
{
int i;
if(mp[x][y]!='.') return false;
if(x>=)//up
for(i=x; i>=; i--)
if(mp[i][y]=='X')
break;
else if(mp[i][y]=='')
return false;
if(x<n)//down
for(i=x; i<n; i++)
if(mp[i][y]=='X')
break;
else if(mp[i][y]=='')
return false;
if(y>=)//left
for(i=y; i>=; i--)
if(mp[x][i]=='X')
break;
else if(mp[x][i]=='')
return false;
if(y<n)//right
for(i=y; i<n; i++)
if(mp[x][i]=='X')
break;
else if(mp[x][i]=='')
return false;
return true;
}
void dfs(int m)
{
int i,j;
for(i=;i<n;i++)
{
for(j=;j<n;j++)
{
if(check(i,j))
{
mp[i][j]='';
dfs(m+);
mp[i][j]='.';
}
}
}
ans=max(ans,m);
}
int main()
{
while(cin>>n&&n)
{
ans=;
for(int i=; i<n; i++)
cin>>mp[i];
dfs();
printf("%d\n",ans);
}
return ;
}

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

  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 - [DFS][二分图最大匹配][匈牙利算法模板][最大流求二分图最大匹配]

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

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

    传送门: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:1000MS     Memory Limit:32768KB   ...

  5. HDU 1045(Fire Net)题解

    以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定大小的棋盘中部分格子存在可以阻止互相攻击的墙,问棋盘中可以放置最多多少个可以横纵攻击炮塔. [题目分析] 这题本来在搜索专题 ...

  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 【连通块的压缩 二分图匹配】

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

  8. HDU 1045 Fire Net 状压暴力

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

  9. HDU 1045 Fire Net 二分图建图

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

随机推荐

  1. c#中关于大对象数组的一些心得

    在之前的一个课题中,曾经需要用到2W行*3W列的float类型矩阵(大约2.4G),由于无法创建大于2G的对象,当时采用了一些取巧的办法回避了,并没有拿出精力来研究一下这个问题.今天和公司的张哥(大牛 ...

  2. DevExpress 学习使用之 ComboBoxEdit

    往 StatusBar 上添加各种部件,似乎都被包装成了 barEditItem 的方式,其 Edit 属性就是具体的部件.以 ComboBoxEdit 为例,关于 ComboBoxEdit 的一些设 ...

  3. C# 添加敏感词

    public class CheckStreamReader { //使用的数据: private static HashSet<string> hash = new HashSet< ...

  4. 使用PetaPoco结合注入实现业务级事务

    使用PetaPoco结合注入实现业务级事务   PetaPoco是一个轻量级ORM,我的MVC项目中使用它结合Repository模式,依靠Unity的生命周期管理对象,保证请求/线程级别的数据上下文 ...

  5. 讲讲Linq to SQL映射(基础篇)

    讲讲Linq to SQL映射(基础篇) 这篇主要讲Linq to  SQL基于属性的映射.即映射数据库,映射表,映射列,映射关系,映射存储过程, 映射函数.然而创建这种映射有三种方法,他们分别是OR ...

  6. Linux学习之系统的构建

    实验环境:ubuntu 12.04 LTS 内核版本:linux-3.9.4 因为一直以来都对Linux的工作机理比较感兴趣,所以正好这两天有机会好好的研究一下,那闲话不多说,直接进入正题. 俗话说的 ...

  7. 关于code reivew

    关于code reivew 先谈谈三个code review的关键因素: 一.创建review要简单 code reivew是一个程序员日常工作中经常做的一件事,理论上来讲,任何一个将要submit到 ...

  8. 最新FFMPEG解码流程

    FFMPEG解码流程: 1. 注册所有容器格式和CODEC:  av_register_all() 2. 打开文件:                    av_open_input_file() 3 ...

  9. HDU 2076 夹角有多大(题目已修改,注意读题)

    Problem Description 时间过的好快,一个学期就这么的过去了,xhd在傻傻的看着表,出于对数据的渴望,突然他想知道这个表的时针和分针的夹角是多少.现在xhd知道的只有时间,请你帮他算出 ...

  10. 【2013Esri全球用户大会精彩看点】ArcGIS 10.2移动产品新特性

    Ø 全新的应用Explorer for ArcGIS 在2013年第四季度,Esri将发布全新的应用Explorer for ArcGIS,它将联合Collector和Operations Dashb ...