HDU 1045 Fire Net(dfs,跟8皇后问题很相似)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1045
Fire Net
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14670 Accepted Submission(s): 8861
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..
....
2
XX
.X
3
.X.
X.X
.X.
3
...
.XX
.XX
4
....
....
....
....
0
1
5
2
4
就是给你一个地图,地图上有一些墙,向地图内放炮台,不要让两个炮台能互相射击到对方,
当然中间有墙就可以阻隔他们。求最多能放多少炮台。
分析:
如果放炮台,在该点设置一个炮台标记,
判断某一点是否可以放炮台,就是从该点向四个方向找,碰到墙停止,碰到别的炮台就返回0,表示该点不能放炮台。
code:
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <string.h>
using namespace std;
char G[][];
int n,sum;
bool check(int x,int y)
{
if(G[x][y]=='X')//该点是墙
return false;
for(int i=x;i<n;i++)//下
{
if(G[i][y]=='X')
break;
if(G[i][y]=='S')
return false;
}
for(int i=x;i>=;i--)//上
{
if(G[i][y]=='X')
break;
if(G[i][y]=='S')
return false;
}
for(int j=y;j<n;j++)//右
{
if(G[x][j]=='X')
break;
if(G[x][j]=='S')
return false;
}
for(int j=y;j>=;j--)//左
{
if(G[x][j]=='X')
break;
if(G[x][j]=='S')
return false;
}
return true;
} void dfs(int x,int y,int num)
{
if(x==n)
{
sum=max(sum,num);
return ;
}
for(int j=y;j<n;j++)
{
if(check(x,j))
{
G[x][j]='S';
dfs(x,j,num+);
G[x][j]='.';
}
}
dfs(x+,,num);
}
int main()
{
while(cin>>n,n)
{
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
cin>>G[i][j];
}
}
sum=;
dfs(,,);
cout<<sum<<endl;
}
return ;
}
HDU 1045 Fire Net(dfs,跟8皇后问题很相似)的更多相关文章
- HDOJ(HDU).1045 Fire Net (DFS)
HDOJ(HDU).1045 Fire Net [从零开始DFS(7)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HD ...
- 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)
Fire Net Problem Description Suppose that we have a square city with straight streets. A map of a ci ...
- 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) ...
- HDU 1045 Fire Net 状压暴力
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 1045 Fire Net 二分图建图
HDU 1045 题意: 在一个n*n地图中,有许多可以挡住子弹的墙,问最多可以放几个炮台,使得炮台不会相互损害.炮台会向四面发射子弹. 思路: 把行列分开做,先处理行,把同一行中相互联通的点缩成一个 ...
随机推荐
- TOJ 1856 Is It A Tree?
Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a ...
- 最强json解析工具
[原]http://blog.csdn.net/xiaoguomumu/article/details/75255629 感觉上面的链接所说,需要传一个T进去,也就是先要构造T,感觉麻烦 可以不这样做 ...
- nginx配置多域名
http{ # 第一个虚拟主机 server { listen 80; server_name aaa.domain.com; #access_log logs/host.access.log mai ...
- git提交代码报错 trailing whitespace的解决方法
1. git提交代码报错 trailing whitespace 禁止执行pre-commit脚本 进入到项目目录中 chmod a-x .git/hooks/pre-commit 2.git提交代码 ...
- 使用Spring的AOP实现切面日志
AOP切面日志的使用方式 @Aspect @Component public class HttpAspect { private static final Logger logger = Logge ...
- 2、Dubbo源码解析--服务发布原理(Netty服务暴露)
一.服务发布 - 原理: 首先看Dubbo日志,截取重要部分: 1)暴露本地服务 Export dubbo service com.alibaba.dubbo.demo.DemoService to ...
- python数据类型(集合)
一.集合概念 集合是一个数学概念:由一个或多个确定的元素所构成的整体叫做集合. 集合中的元素三个特征: 确定性(元素必须可hash) 互异性(去重)——将一个列表变为集合,就自动去重了 无序性(集合中 ...
- 从零开始的全栈工程师——underscore
underscore 是js封装的一个js库 库和框架是有区别的 mvc框架就是backbone就是依赖这个库underscore就是定义了一个_( 下划线对象 ); 函数库的所有的方法都归属于这个对 ...
- BZOJ3033: 太鼓达人(欧拉回路)
题意 题目链接 Sol 第一问的答案是\(2^M\),因为每个位置只有\(0 / 1\)两种情况,最优情况下一定是每个位置代表着一个长度为\(K\)的字符串 考虑相邻两个字符串之间的转化,第二个字符串 ...
- setTimeout的实现原理以及setTimeout(0)的使用场景
先看一段代码: var start = new Date(); setTimeout(function(){ var end = new Date(); console.log("Tim ...