HDU 1045 Fire Net(DFS 与8皇后问题类似)
Fire Net
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14780 Accepted Submission(s): 8932
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
题目意思:
题目给出一个n和一幅N*N的图,图中包含'.','X'两种字符,
要求在上面放上碉堡,碉堡能攻击他所在的行和列,但不能攻击墙,求最多能放多少个碉堡.
X是墙,不能放炮台
与八皇后类似,炮台不能存在于同行同列,除非中间隔墙
直接dfs
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 k)
{
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
if(check(i,j))
{
G[i][j]='S';
dfs(k+);
G[i][j]='.';
}
}
}
if(sum<k)
sum=k;
}
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(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) ...
- 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地图中,有许多可以挡住子弹的墙,问最多可以放几个炮台,使得炮台不会相互损害.炮台会向四面发射子弹. 思路: 把行列分开做,先处理行,把同一行中相互联通的点缩成一个 ...
随机推荐
- ubuntu基本配置学习(1)
[转载]源自:http://www.haogongju.net/art/2048612 附加另外一篇文章:http://www.cnblogs.com/vincent-hv/archive/2013/ ...
- zookeeper【4】master选举
考虑7*24小时向外提供服务的系统,不能有单点故障,于是我们使用集群,采用的是Master+Slave.集群中有一台主机和多台备机,由主机向外提 供服务,备机监听主机状态,一旦主机宕机,备机必需迅速接 ...
- select下拉框选择字体大小
效果: 结合Bootstrap.jQuery和ES6字符串模板与箭头函数使用JavaScript DOM操作动态添加option,随着option:selected选中的字号而改变相应的字体大小 代码 ...
- Codeforces Round #394 (Div. 2)
前一半是刚刚打完比赛的时候写的……不知为啥手腕有点僵,估计是前一个小时用力过度了吧= = 前四题看着还好,后两题就有点懵逼了……现在还不知道E题的题意到底是啥…… 不管了……还没找着官方题解,贴一下自 ...
- 在URL里传入数组到HTML 里。
需求 静态页面根据URL输入,动态显示图表满足如下两个条件. 1. 隐藏指定的行 2. 设定初始显示的Check box 需要的部分被打勾 实现 1. 创建一个静态的页面, <table id= ...
- 04_dubbo_ioc
[dubbo的IOC实现方法] dubbo的IOC具体实现在:T injectExtension( T instance )方法中,该方法在3个地方被使用: ExtensionLoader.getEx ...
- Ta们,用云计算改变着更多普通人的生活,所以,我们1218
维族音乐的传承者:为家园建设生态农业:为50万货运司机谋福利:电视游戏行业复兴的倡导者:......还有很多平凡普通的人,不同的主角.不同的情节,用自己的云上轨迹在点滴改变着我们的周遭世界.所以,我们 ...
- VC添加文件到工程出错问题--FileTool.dll
原文:http://blog.csdn.net/bingdianlanxin/article/details/45112737 在我们的软件开发中,经常需要导入其他文件到我们的工程. 一般,我们会选择 ...
- python(day1-11)
day1:Python入门 day2:数字类型字符编码 day3:函数 day4:模块与包 day5:常用模块 day6:面向对象 day8:异常处理 day9:网络编程 day10:并发编程 day ...
- lua的面向对象实现
百度搜索一下,给出出的解决方案和学习帖子很多,可是我还是有很多的问题! (1)什么是面向对象? (2)lua中怎么实现面向对象? (3)什么样的实现既简单又能完成我的功能? (4)一定要按照c++的方 ...