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

Problem Description
Suppose that we have a square city with straight
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.

 
Input
The input file contains one or more map descriptions,
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.
 
Output
For each test case, output one line containing the
maximum number of blockhouses that can be placed in the city in a legal
configuration.
 
Sample Input
4
.X..
....
XX..
....
2
XX
.X
3
.X.
X.X
.X.
3
...
.XX
.XX
4
....
....
....
....
0
 
Sample Output
5
1
5
2
4
 
Source
 
Recommend
We have carefully selected several similar problems for
you:  1016 1051 1050 1175 1241 
 
题解:
         用深度优先搜索,按顺序先找一个点,标记这个点的位置,再找下一个符合要求的点,直到找不到为止;
   
1. 
     用于判断是否可以放:
    可以放就找下一个;
                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的更多相关文章

  1. hdu 1045 Fire Net(最小覆盖点+构图(缩点))

    http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit:1000MS     Memory Limit:32768KB   ...

  2. HDU 1045(Fire Net)题解

    以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定大小的棋盘中部分格子存在可以阻止互相攻击的墙,问棋盘中可以放置最多多少个可以横纵攻击炮塔. [题目分析] 这题本来在搜索专题 ...

  3. HDOJ(HDU).1045 Fire Net (DFS)

    HDOJ(HDU).1045 Fire Net [从零开始DFS(7)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HD ...

  4. HDU 1045——Fire Net——————【最大匹配、构图、邻接矩阵做法】

    Fire Net Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  5. HDU 1045 Fire Net 二分图建图

    HDU 1045 题意: 在一个n*n地图中,有许多可以挡住子弹的墙,问最多可以放几个炮台,使得炮台不会相互损害.炮台会向四面发射子弹. 思路: 把行列分开做,先处理行,把同一行中相互联通的点缩成一个 ...

  6. HDU 1045 (DFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1045 题目大意:在不是X的地方放O,所有O在没有隔板情况下不能对视(横行和数列),问最多可以放多少个 ...

  7. HDU - 1045 Fire Net(二分匹配)

    Description Suppose that we have a square city with straight streets. A map of a city is a square bo ...

  8. HDU - 1045 Fire Net(搜索)

    Description Suppose that we have a square city with straight streets. A map of a city is a square bo ...

  9. HDU 1045 - Fire Net - [DFS][二分图最大匹配][匈牙利算法模板][最大流求二分图最大匹配]

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1045 Time Limit: 2000/1000 MS (Java/Others) Mem ...

随机推荐

  1. C语言结构体变量私有化

    操作系统 : CentOS7.3.1611_x64 gcc版本 :4.8.5 问题描述 C语言结构体定义中的变量默认是公有(Public)属性,如果实现成员变量的私有(Private)化? 解决方案 ...

  2. 如何修改启动jupyter的文件路径

    1.cmd 2.jupyter notebook 工作目录路径 办法二: 1.启动pycharm 2.创建一个ipynb文件 3.运行该文件---在打印结果中找到网址,在网页中打开即可正常显示

  3. 读取mysql数据库的数据,转为json格式

    # coding=utf-8 ''' Created on 2016-10-26 @author: Jennifer Project:读取mysql数据库的数据,转为json格式 ''' import ...

  4. Unitek的USB3.0 TF卡读卡器

    淘宝买了个Unitek的usb3.0读卡器, 用来换掉之前用了很久sks的sub2读卡器, 收到之后在Ubuntu下先测了一下, 发现识别出来的是usb2.1 lsusb -D /dev/bus/us ...

  5. PHP操作二进制字节数据

    在PHP开发中大都是操作字符类数据,极为方便,但操作二进制又如何呢,下面代码举例看看. 函数:  pack(format,args+) pack()和unpack()函数的第一个参数表如下 Bash ...

  6. (8) MySQL主从复制架构使用方法

    一. 单个数据库服务器的缺点 数据库服务器存在单点问题 数据库服务器资源无法满足增长的读写请求 高峰时数据库连接数经常超过上限 二. 如何解决单点问题 增加额外的数据库服务器,组建数据库集群 同一集群 ...

  7. php项目踩到的empty函数的一个坑

    报错信息: PHP Fatal error: Can't use function return value in write context in /目录省略.../XXService.php on ...

  8. netty 的 Google protobuf 开发

    根据上一篇博文 Google Protobuf 使用 Java 版 netty 集成 protobuf 的方法非常简单.代码如下: server package protobuf.server.imp ...

  9. laravel5.8笔记十:Redis操作

    > 位置:\vendor\laravel\framework\src\Illuminate\Redis\Connections\PhpRedisConnection.php > 参考:ht ...

  10. [转] C#中的delegate 和 event

    转至:here 终于会用c#中的delegate(委托) 作者:qq826364410 引言 Delegate是Dotnet1.0的时候已经存在的特性了,但由于在实际工作中一直没有机会使用Delega ...