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. ExpandoObject,DynamicObject,DynamicMetaObject

    ExpandoObject,DynamicObject,DynamicMetaObject 接上文:浅谈Dynamic关键字系列之三(上) 为什么TryXXX方法没有被调用?? 将DynamicPro ...

  2. Webapi帮助文档

    生成自己的Webapi帮助文档(一) 最近Webapi接口的开发刚刚进入尾声,随之而来的是让用户知道接口的详细参数信息,看过淘宝的接口文档,但网上没找到他的实现方式 虽然新建Webapi时C#也会给你 ...

  3. 解决Xcode升级7.0后,部分.a静态库在iOS9.0的模拟器上,link失败的问题

    简单描述一下这个问题:我们项目中使用了Google大神开发的LevelDB键值对数据库,在Xcode6,iOS8的环境下,编译好的.a静态库是可以正常使用的.但是升级后,发现在模拟器上无法link成功 ...

  4. [Usaco2007 Dec]宝石手镯[01背包][水]

    Description 贝茜在珠宝店闲逛时,买到了一个中意的手镯.很自然地,她想从她收集的 N(1 <= N <= 3,402)块宝石中选出最好的那些镶在手镯上.对于第i块宝石,它的重量为 ...

  5. 企业架构与建模之Archimate视图和视角

    企业架构与建模之Archimate视图和视角 3. ArchiMate的视角与视图 创建.维护一个企业架构是一件非常复杂繁琐的事情,因为这项工作需要面对许多背景.利益各异的干系人,对他们所关注的问题进 ...

  6. Navicat Premium 11.0.10破解补丁

    Navicat Premium 11.0.10破解补丁   Navicat Premium 是一个可多重连接的数据库管理工具,让你以单一程序同時连接到 MySQL.SQL Server.SQLite. ...

  7. IOS学习之路七(通过xib自定义UITableViewCell)

    一.新建iOS Application工程,选择Single View Application,不要选中Use Storyboard.假设指定的是product name是:UITableViewCe ...

  8. 如何在大型的并且有表分区的数据库中进行DBCC CHECKDB操作

    如何在大型的并且有表分区的数据库中进行DBCC CHECKDB操作 其实这个问题已经在<SQLSERVER企业级平台管理实践>里徐老师已经讲过了,不过我想用自己的语言再讲详细一些 笔记链接 ...

  9. 没有标准化字符串的后果(IOS)

    对于NSString肯定会经常用到,谈谈最近在项目中遇到的一个奇特的现象.如下:我们知道文件系统的命名都是用的字符串,比如你给文件取名“a.pdf”,然后保存文件后,那个文件的名字就真的是 " ...

  10. jQuery表格排序组件-tablesorter

    一.引入文件 <script type="text/javascript" src="js/jquery.js"></script> & ...