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. 比Spring简单的IoC容器

    比Spring简单的IoC容器 Spring 虽然比起EJB轻量了许多,但是因为它需要兼容许多不同的类库,导致现在Spring还是相当的庞大的,动不动就上40MB的jar包, 而且想要理解Spring ...

  2. ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统

    构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(30)-本地化(多语言)   我们的系统有时要扩展到其他国家,或者地区,需要更多的语言环境,微软提供了一些解决方 ...

  3. 网站部署到Windows Azure Website上

    使用CSDN Code将网站部署到Windows Azure Website上 在云计算时代,开发和部署应该是完全统一和集成的.在海外,开发者可以用github来管理他们的代码,并且直接部署到Wind ...

  4. 教你用shell写CGI程序

    以前用shell写过一些cgi的例子.今天向大家介绍一下. CGI是一种接口的标准,并不区分编程语言,也就是说,CGI可以用任何一种语言编写,只要这种语言具有标准输入.输出和环境变量.CGI会将标准输 ...

  5. 企业架构研究总结(39)——TOGAF架构能力框架之架构委员会和架构合规性

    3. 架构委员会 正如前面所说,一个用来对架构治理策略的实现进行监督的跨组织的架构委员会是架构治理策略成功的主要要素之一.架构委员会应该能够代表所有主要干系人的需求,并且通常还需要对整个架构的审查及维 ...

  6. ngx-push-stream模块源码学习(四)——订阅

    一.概述 push stream模块允许三种模式的订阅者: longpolling:每收到服务端响应数据即断开连接然后迅速重连,连接耗时可以忽略 stream:与服务端保持长连接,持续不断的请求-&g ...

  7. CLR执行模型与常见的几个概念

    CLR执行模型与常见的几个概念 一.CLR执行模型 1.1从代码到程序集 CLR执行的模型是从代码开始,经过编译器生成托管模块,默认情况下可以让多个托管模块和资源生成程序集.下图为其大致过程. 1.2 ...

  8. Lucene.net入门学习系列(2)

    Lucene.net入门学习系列(2) Lucene.net入门学习系列(1)-分词 Lucene.net入门学习系列(2)-创建索引 Lucene.net入门学习系列(3)-全文检索 在使用Luce ...

  9. U盘读不出+卷标丢失+像读卡器+大小0+无媒体

    U盘读不出+卷标丢失+像读卡器+大小0+无媒体 标题有点怪,原因是我不都不知道该怎样概括这个鸟问题,所以尽可能列出一些主要现象,希望有需要的童鞋搜到.但比标题更怪的是问题本身,且听我道来: 陪伴我若干 ...

  10. flask 真是太棒啦,阅读手册后就能做出一个博客了

    真是很好的东西,有很多有益处的东西. 有template引擎, 有flask自己带的g (用来处理访问与数据库打开关闭的) 有flask自己的处理session的功能 自带的jinja2模板引擎也是比 ...