Fire Net

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5863    Accepted Submission(s): 3280

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
.X..
....
XX..
.... XX
.X .X.
X.X
.X. ...
.XX
.XX ....
....
....
....
Sample Output
5
1
5
2
4
 
Source
 
Recommend
We have carefully selected several similar problems for you:  1050 1067 1258 1053 1789 

 
  DFS深搜经典题
  暴力穷举即可,数据规模大的话据说可以用二分匹配,另外这道题也可以用贪心来做。
  代码: 
 
 #include <iostream>

 using namespace std;
char a[][];
int cnt,n;
bool judge(int x,int y) //判断这一步可不可以走
{
if(a[x][y]=='X')
return ;
if(a[x][y]=='*')
return ;
int i;
//判断这一行上有无碉堡
for(i=y;i>=;i--){
if(a[x][i]=='*')
return true;
if(a[x][i]=='X')
break;
}
for(i=y;i<=n;i++){
if(a[x][i]=='*')
return true;
if(a[x][i]=='X')
break;
}
//判断这一列上有无碉堡
for(i=x;i>=;i--){
if(a[i][y]=='*')
return ;
if(a[i][y]=='X')
break;
}
for(i=x;i<=;i++){
if(a[i][y]=='*')
return ;
if(a[i][y]=='X')
break;
}
//可以走
return ;
}
void dfs(int cx,int cy,int cn)
{
if(cn>cnt) cnt=cn; //记录最大值
int x=-,y=-;
int i,j;
for(i=cx;i<=n;i++) //选择这一步的位置
for(j=;j<=n;j++){
if(i==cx && j<=cy)
continue;
if(judge(i,j))
continue;
x=i;y=j;
a[x][y] = '*';
dfs(x,y,cn+);
a[x][y] = '.';
}
if(x==- && y==-)
return ;
}
int main()
{
while(cin>>n){
if(n==) break;
int i,j;
for(i=;i<=n;i++) //输入地图
for(j=;j<=n;j++)
cin>>a[i][j];
cnt = ;
dfs(,,); //深搜
cout<<cnt<<endl;
}
return ;
}

Freecode : www.cnblogs.com/yym2013

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 1728 逃离迷宫(DFS经典题,比赛手残写废题)

    逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  3. HDU 1045 Fire Net(DFS)

    Fire Net Problem Description Suppose that we have a square city with straight streets. A map of a ci ...

  4. HDU 1045 - Fire Net - [DFS][二分图最大匹配][匈牙利算法模板][最大流求二分图最大匹配]

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

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

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

  6. hdu 1045 Fire Net(最小覆盖点+构图(缩点))

    http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit:1000MS     Memory Limit:32768KB   ...

  7. HDU 1045(Fire Net)题解

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

  8. HDU 1045——Fire Net——————【最大匹配、构图、邻接矩阵做法】

    Fire Net Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  9. HDU 1045 Fire Net 状压暴力

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

随机推荐

  1. Chord算法

    转自:http://blog.csdn.net/wangxiaoqin00007/article/details/7374833 虽然网上搜索CHord,一搜一大堆,但大多讲得不太清楚明白.今天发现一 ...

  2. MySQL使用痕迹清理~/.mysql_history

    mysql会给出我们最近执行的SQL命令和脚本:同linux command保存在~/.bash_history一样,你用mysql连接MySQL server的所有操作也会被记录到~/.mysql_ ...

  3. tcpdump wireshark 实用过滤表达式(针对ip、协议、端口、长度和内容) 实例介绍

    tcpdump wireshark 实用过滤表达式(针对ip.协议.端口.长度和内容) 实例介绍 标签: 网络tcpdst工具windowslinux 2012-05-15 18:12 3777人阅读 ...

  4. l来自wentao:项目加入缓存(redis),实时调试时用 -----可视化缓存,flushdb

    下文来自segmentfault,网站:一个交流网站:https://segmentfault.com/ 在做一个项目时如果加入缓存(例如redis),我如何进行实时调试呢? 缓存 高并发 架构 gz ...

  5. TopCoder SRM 590

     第一次做TC,不太习惯,各种调试,只做了一题...... Problem Statement     Fox Ciel is going to play Gomoku with her friend ...

  6. [Effective JavaScript 笔记]第19条:熟练掌握高阶函数

    高阶函数介绍 高阶函数曾经是函数式编程的一个概念,感觉是很高深的术语.但开发简洁优雅的函数可以使代码更加简单明了.过去几年中脚本语言采用了这些个技术,揭开了函数式编程的最佳惯用法的神秘面纱.高阶函数就 ...

  7. ASP注入靶机

     ASP:   <%  Dim Db,MyDbPath dim conn '可修改设置一:========================定义数据库类别,1为SQL数据库,0为Access数据库 ...

  8. 【云计算】Dockerfile、镜像、容器快速入门

    Dockerfile.镜像.容器快速入门 1.1.Dockerfile书写示例 Dockerfile可以用来生成Docker镜像,它明确的定义了Image的生成过程.虽然直接修改容器也可以提交生成镜像 ...

  9. 【云计算】docker build如何支持参数化构建?

    docker 1.9.0版本之后,已经支持docker build参数化构建. docker 版本更新记录: github讨论: 参开资料: https://github.com/docker/doc ...

  10. makefile中的自动化变量$@,$%,$

    转自:http://www.2cto.com/os/201302/191344.html   makefile中的自动化变量$@,$%,$   自动化变量  模式规则中,规则的目标和依赖文件名代表了一 ...