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. Oracle 18C DBCA建库报ora-01012错误

    操作系统:rhel 7.2 解决方案: 1).设置/etc/systemd/logind.conf中RemoveIPC=no2).重启服务器或者重启systemd-logind重启systemd-lo ...

  2. Python定期删除文件、整理文件夹

    1.根据传入的参数,文件所在目录,匹配文件的正则表达式,过期天数进行删除,这些可写在配置文件del_file.conf. del_file3.py #!/usr/bin/env python # en ...

  3. python 信息同时输出到控制台与文件

    python编程中,往往需要将结果用print等输出,如果希望输出既可以显示到IDE的屏幕上,也能存到文件中(如txt)中,该怎么办呢? 方法1 可通过日志logging模块输出信息到文件或屏幕.但可 ...

  4. [转]浅谈Normalize.css

    原文地址:https://www.jianshu.com/p/3d21d1932aa0 Normalize.css只是一个很小的css文件,但它在默认的HTML元素样式上提供了跨浏览器的高度一致性.相 ...

  5. Unity输出PC端(Windows) 拖拽文件到app中

    需求:给策划们写一个PC端(Window)的Excel导表工具.本来用OpenFile打开FileExplorerDialog后让他们自己选择想要添加的Excel文件就行了,结果有个需求是希望能拖拽E ...

  6. docker容器添加微软雅黑字体

    添加中文字体其实很简单,往容器里COPY一个ttf字体文件就生效了,不需要执行fc-cache. 基于debian 8的tomcat容器,Dockerfile: COPY msyh.ttf /usr/ ...

  7. C++ 非常量引用无效

    /* 非常量引用无效 */ #include <iostream> using namespace std; /* C++标准的规定:非常量的引用不能指向临时对象: 为了防止给常量或临时变 ...

  8. linux系统下键盘按键的重新映射——xmodmap工具和xev工具

    大家会不会有时候,感觉键盘上的某几个键用起来不是很方便,打字打久了很容易手指头疼呢? 例如大家使用vim编辑器时, 经常需要使用到esc键,而该键在左上角,很不方便的.再比如写程序的时候,经常会使用到 ...

  9. 【QT】二进制读取图像文件测试

    QDataStream in(&file); int n; in >> n ; file.close(); qDebug() << n<<"en& ...

  10. 微信的NATIVE支付提示201商户订单号重复的解决方案

    无论采取模式一还是模式二,进行预支付ID获取的时候应当确保订单号的唯一性,否则就会造成第二次扫码后的重复提醒. 解决方案: 以预支付ID处理为例: 商城: 1.创建log_id数据表如:out_tra ...