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) ...
随机推荐
- yii增删改查
一.查询数据集合 1.$admin=Admin::model()->findAll($condition,$params);//该方法是根据一个条件查询一个集合,如: findAll( ...
- 突破XSS字符数量限制执行任意JS代码
一.综述 有些XSS漏洞由于字符数量有限制而没法有效的利用,只能弹出一个对话框来YY,本文主要讨论如何突破字符数量的限制进行有效的利用,这里对有效利用的定义是可以不受限制执行任意JS.对于跨站师们来说 ...
- mysql 索引 详解
索引是快速搜索的关键.MySQL索引的建立对于MySQL的高效运行是很重要的.下面介绍几种常见的MySQL索引类型. 在数据库表中,对字段建立索引可以大大提高查询速度.假如我们创建了一个 mytabl ...
- php url地址重写
地址重写: urlRewrite: 就是: 1. 将php的地址index.php不写只写Action模块和function方法, 或者 2. php地址转变成html地址, 就是一种假的html, ...
- Linux建立软连接
实例:ln -s /home/gamestat /gamestat linux下的软链接类似于windows下的快捷方式 ln -s a b 中的 a 就是源文件,b是链接文件名,其作用是当进入 ...
- SSRS匿名访问
---本人数据库是SqlServer2008 R2 匿名访问Reporting Service 2008 我想通过访问Url的方式,把部署到Sql Server Reporting Service ...
- cocos2d-x 在android环境下开发遇到的一些bug
今天在弄一个关于android环境下解析xml的东东,遇到了2个比较麻烦问题 1.android的apk下文件是压缩文件,io.open模式无法读取到数据的, 解决思路就是: CCFileUtils: ...
- hibernate操作数据库例子
1.工程目录结构如下 2.引入需要的jar包,如上图. 3.创建持久化类User对应数据库中的user表 package com.hibernate.配置文件.pojo; import java.sq ...
- java笔记--查看和修改线程名称
查看和修改线程名称 --如果朋友您想转载本文章请注明转载地址"http://www.cnblogs.com/XHJT/p/3893797.html "谢谢-- java是一种允许 ...
- js获取中国日期-农历
/* var bsYear; var bsDate; var bsWeek; var arrLen=8; //数组长度 var sValue=0; //当年的秒数 var dayiy=0; //当年第 ...