hdu 1045:Fire Net(DFS经典题)
Fire Net
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5863 Accepted Submission(s): 3280
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.
.X..
....
XX..
.... XX
.X .X.
X.X
.X. ...
.XX
.XX ....
....
....
....
#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经典题)的更多相关文章
- HDOJ(HDU).1045 Fire Net (DFS)
HDOJ(HDU).1045 Fire Net [从零开始DFS(7)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HD ...
- HDU 1728 逃离迷宫(DFS经典题,比赛手残写废题)
逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- HDU 1045 Fire Net(DFS)
Fire Net Problem Description Suppose that we have a square city with straight streets. A map of a ci ...
- HDU 1045 - Fire Net - [DFS][二分图最大匹配][匈牙利算法模板][最大流求二分图最大匹配]
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1045 Time Limit: 2000/1000 MS (Java/Others) Mem ...
- HDU 1045 Fire Net(dfs,跟8皇后问题很相似)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 1045 Fire Net(最小覆盖点+构图(缩点))
http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit:1000MS Memory Limit:32768KB ...
- HDU 1045(Fire Net)题解
以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定大小的棋盘中部分格子存在可以阻止互相攻击的墙,问棋盘中可以放置最多多少个可以横纵攻击炮塔. [题目分析] 这题本来在搜索专题 ...
- HDU 1045——Fire Net——————【最大匹配、构图、邻接矩阵做法】
Fire Net Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- HDU 1045 Fire Net 状压暴力
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others) ...
随机推荐
- vc++ 6.0下Glut的配置 及 Glut 框架介绍
2014-04-08 16:18:30 一.配置Glut 学习来源: http://blog.sina.com.cn/s/blog_5f0cf7bd0100c9oa.html 亲测可行. Glut的 ...
- .Net开源工作流Roadflow的使用与集成(转)
序言 最近公司要整理公司内部oa系统,需要使用到工作流,所以就开始了开源工作流挑选,使用,到集成到公司内部系统的工作. 首先在网上搜了文档,自己也有补充,整理啦国内几款工作流的比较,由于没有个个击破式 ...
- stripslashes — 反引用一个引用字符串
stripslashes (PHP 4, PHP 5) stripslashes — 反引用一个引用字符串 Report a bug 说明 string stripslashes ( string ...
- SpringMVC利用拦截器防止SQL注入
引言 随着互联网的发展,人们在享受互联网带来的便捷的服务的时候,也面临着个人的隐私泄漏的问题.小到一个拥有用户系统的小型论坛,大到各个大型的银行机构,互联网安全问题都显得格外重要.而这些网站的背后,则 ...
- cocos代码研究(1)sprite学习笔记
各种方法创建Sprite和Animate //图片创建法 参数一:图片资源路径 参数二:Rect选区 auto sprite = Sprite::create(, )); addChild(sprit ...
- 一起做RGB-D SLAM (1)
前言 2016.11 更新 增加了对16.04的支持. 整理了过时的代码. SLAM,即Simultaneous Localization and Mapping,中文译作同时定位与地图创建,是近几十 ...
- window.parent与window.openner 之前的总结
今天总结一下js中几个对象的区别和用法: 1.首先来说说 parent.window与top.window的用法 "window.location.href","loca ...
- 【云计算】Kubernetes、Marathon等框架需要解决什么样的问题?
闲谈Kubernetes 的主要特性和经验分享 Capitalonline全球云主机.全球私有网络,免费试用进行时 » 主要介绍 Kubernetes 的主要特性和一些经验.先从整体上 ...
- 【Hibernate】Hibernate系列3之配置文件详解
配置文件详解 3.1.配置文件 连接池性能优化:http://www.cnblogs.com/xdp-gacl/p/4002804.html
- JavaScript Math 对象方法
Math 对象方法 方法 描述 abs(x) 返回数的绝对值. acos(x) 返回数的反余弦值. asin(x) 返回数的反正弦值. atan(x) 以介于 -PI/2 与 PI/2 弧度之间的数值 ...