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) ...
随机推荐
- css 文字换行
换行word-wrap: break-word 新起一行对长单词进行断句word-break:break-all 本行对长单词进行断句 不换行,强制一行white-space:nowrap table ...
- Visual Studio Online Integrations-Customer support
原文:http://www.visualstudio.com/zh-cn/explore/vso-integrations-directory-vs
- linux useradd(adduser)命令参数及用法详解(linux创建新用户命令)
linux useradd(adduser)命令参数及用法详解(linux创建新用户命令) useradd可用来建立用户帐号.帐号建好之后,再用passwd设定帐号的密码.而可用userdel删除帐号 ...
- CSS3 transforms 3D翻开
R T L B <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...
- Spark-1.0.0 standalone分布式安装教程
Spark目前支持多种分布式部署方式:一.Standalone Deploy Mode:二Amazon EC2.:三.Apache Mesos:四.Hadoop YARN.第一种方式是单独部署,不需要 ...
- bc.34.B.Building Blocks(贪心)
Building Blocks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- Java 参数的一些心得
java 对象入参是传入的是引用(一块内存), 基础类型是值(复制内容),测试代码如下 public class TestA { private String name; public String ...
- 多线程 or 多进程 (转强力推荐)
在Unix上编程采用多线程还是多进程的争执由来已久,这种争执最常见到在C/S通讯中服务端并发技术 的选型上,比如WEB服务器技术中,Apache是采用多进程的(perfork模式,每客户连接对应一个进 ...
- Android--UI之EditText
前言 上一篇博客介绍了Android的TextView控件,这篇博客来说一下EditText控件.EditText为一个文本控件,提供了文本输入的功能,而且继承自TextView,可以理解为可以输入的 ...
- [Effective JavaScript 笔记]第34条:在原型中存储方法
js中完全有可能不借助原型进行编程.不用在其原型中定义任何的方法. 创建对象 构造函数法 所有属性和方法都在构造函数中定义 function User(name,pwd){ this.name=nam ...