hdu 1045
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): 16485 Accepted Submission(s):
10033
streets. A map of a city is a square board with n rows and n columns, each
representing a street or a piece of wall.
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.
followed by a line containing the number 0 that signals the end of the file.
Each map description begins with a line containing a positive integer n that is
the size of the city; n will be at most 4. The next n lines each describe one
row of the map, with a '.' indicating an open space and an uppercase 'X'
indicating a wall. There are no spaces in the input file.
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
flag=;
//space!!;
for(int i=;i<;i++)
{
int tw=w, tk=k;// 重点!!!!!!! 每次判断是从k,w对应的点开始; 如果把( int tw=w, tk=k;) 放在space!!处,
// 会导致 第二个方向搜索的起点是第一个方向搜索的终点;
while()
{
tw=tw+next[i][]; tk=tk+next[i][];
if(book[tk][tw]==)//遇到边界
{
break;
}
else if(book[tk][tw]==)//遇到墙
{
break;
}
else if(book[tk][tw]==)//遇到碉堡
{
flag=;
break;
}
//tw=tw+next[i][1]; tk=tk+next[i][0];
}
}
if(flag==)//
{
book[k][w]=;
dfs(num+);
book[k][w]=;
}
#include<cstdio>
#include<cstring> using namespace std; int n;
char str[];
int book[][];
int max;
int flag;
int next[][]={{,},{,},{-,},{,-}}; void dfs(int num)//深度优先搜索 ,以放的数量为参数递归,一直递归到不能放为止;
{
//printf("%d\n",num);
if(max<num)
{
max=num;//每次递归更新数据,从而得到最大值;
}
for(int k=;k<=n;k++)
{
for(int w=;w<=n;w++)
{
if(book[k][w]==)//表示为空;
{
flag=;
//space!!;
for(int i=;i<;i++)
{
int tw=w, tk=k;// 重点!!!!!!! 每次判断是从k,w对应的点开始; 如果把( int tw=w, tk=k;) 放在space!!处,
// 会导致 第二个方向搜索的起点是第一个方向搜索的终点;
while()
{
tw=tw+next[i][]; tk=tk+next[i][];
if(book[tk][tw]==)//遇到边界
{
break;
}
else if(book[tk][tw]==)//遇到墙
{
break;
}
else if(book[tk][tw]==)//遇到碉堡
{
flag=;
break;
}
//tw=tw+next[i][1]; tk=tk+next[i][0];
}
}
if(flag==)//
{
book[k][w]=;
dfs(num+);
book[k][w]=;
}
}
}
}
} int main()
{
while(~scanf("%d",&n))
{
if(n==)
{
return ;
}
memset(book,,sizeof(book));//初始化,使没有赋值的都为零;
for(int i=;i<=n;i++)
{
getchar();
scanf("%s",str);
for(int j=;j<n;j++)
{
if(str[j]=='.')
{
book[i][j+]=;//记录空的地方;
}
else if(str[j]=='X')
{
book[i][j+]=;//记录有墙的地方;
}
}//book[i][j]=3记录碉堡;book[i][j]=0表示边界;
}
max=;
dfs();
printf("%d\n",max);
}
return ;
}
hdu 1045的更多相关文章
- 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)题解
以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定大小的棋盘中部分格子存在可以阻止互相攻击的墙,问棋盘中可以放置最多多少个可以横纵攻击炮塔. [题目分析] 这题本来在搜索专题 ...
- 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——————【最大匹配、构图、邻接矩阵做法】
Fire Net Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- HDU 1045 Fire Net 二分图建图
HDU 1045 题意: 在一个n*n地图中,有许多可以挡住子弹的墙,问最多可以放几个炮台,使得炮台不会相互损害.炮台会向四面发射子弹. 思路: 把行列分开做,先处理行,把同一行中相互联通的点缩成一个 ...
- HDU 1045 (DFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1045 题目大意:在不是X的地方放O,所有O在没有隔板情况下不能对视(横行和数列),问最多可以放多少个 ...
- HDU - 1045 Fire Net(二分匹配)
Description Suppose that we have a square city with straight streets. A map of a city is a square bo ...
- HDU - 1045 Fire Net(搜索)
Description Suppose that we have a square city with straight streets. A map of a city is a square bo ...
- HDU 1045 - Fire Net - [DFS][二分图最大匹配][匈牙利算法模板][最大流求二分图最大匹配]
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1045 Time Limit: 2000/1000 MS (Java/Others) Mem ...
随机推荐
- Deep Learning.ai学习笔记_第四门课_卷积神经网络
目录 第一周 卷积神经网络基础 第二周 深度卷积网络:实例探究 第三周 目标检测 第四周 特殊应用:人脸识别和神经风格转换 第一周 卷积神经网络基础 垂直边缘检测器,通过卷积计算,可以把多维矩阵进行降 ...
- CouchDB 简单HTTP接口使用说明
目录 1.简介 2.安装 2.HTTP接口简单使用 2.1.认证接口 2.1.1 Basic Authentication 2.1.2 Cookie Authentication 2.2 创建与删除数 ...
- Fiddler抓包_重定向AutoResponder
1. 文件及图片替换(Enable rules) 目的:允许从本地返回文件,代替服务器响应,而不用将文件发布到服务器[可用正式环境验证本地文件] 步骤一:抓页面http://ir.baidu.com/ ...
- Docker Mongo数据库主从同步配置方法
一.具体操作方法 1.启两个Mongo容器 docker run --name mongo1 -p 21117:27017 -d mongo --noprealloc --smallfiles --r ...
- 【C++】C++中的流
目录结构: contents structure [-] 1.IO类 IO对象无拷贝状态 条件状态 文件流 文件模式 string流 1.IO类 除了istream和ostream之外,标准库还定义了 ...
- TensorFlow+Keras 02 深度学习的原理
1 神经传递的原理 人类的神经元传递及其作用: 这里有几个关键概念: 树突 - 接受信息 轴突 - 输出信息 突触 - 传递信息 将其延伸到神经元中,示意图如下: 将上图整理成数学公式,则有 y = ...
- 使用idea对spring boot项目打jar和war包[文件]
pom.xml文件编写 打JAR包时 <groupId>com.netcorner</groupId> <artifactId>demo</artifactI ...
- JAVA方法中的参数用final来修饰的原因
JAVA方法中的参数用final来修饰的原因 很多人都说在JAVA中用final来修饰方法参数的原因是防止方法参数在调用时被篡改,其实也就是这个原因,但理解起来可能会有歧义,有的人认为是调用语句的 ...
- Django Rest Framework(认证、权限、限制访问频率)
阅读原文Django Rest Framework(认证.权限.限制访问频率) django_rest_framework doc django_redis cache doc
- [Memcached]分布式缓存系统Memcached在Asp.net下的应用
Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度.Memcached ...