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. 每天一个linux命令(16):which

    1.命令简介 which (which) 命令的作用是在PATH变量指定的路径中搜索某个系统命令的位置并且返回第一个搜索结果.也就是说使用which命令就可以看到某个系统命令是否存在以及执行的到底是哪 ...

  2. linux清除缓存

    linux清除缓存:需要root权限$ sync$ echo 3 >/proc/sys/vm/drop_caches 上面的echo 3 是清理所有缓存 echo 0 是不释放缓存 echo 1 ...

  3. 【redis持久化】redis持久化理解

    1.以下内容仅为个人理解和总结,仅供参考,万万不可全盘真信,内容会进行实时改进和修正 2.redis持久化: 参考链接1.https://redis.io/topics/persistence  -- ...

  4. notepad++ 复制代码--高亮 - 带颜色

    思路来源:http://blog.csdn.net/super828/article/details/72826024 选择代码,然后右键选择菜单命令

  5. linux驱动面试题整理

    1.字符型驱动设备你是怎么创建设备文件的,就是/dev/下面的设备文件,供上层应用程序打开使用的文件? 答:mknod命令结合设备的主设备号和次设备号,可创建一个设备文件. 评:这只是其中一种方式,也 ...

  6. Unity应用架构设计(7)——IoC工厂理念先行

    一谈到 『IoC』,有经验的程序员马上会联想到控制反转,将创建对象的责任反转给工厂.IoC是依赖注入 『DI』 的核心,大名鼎鼎的Spring框架就是一个非常卓越的的控制反转.依赖注入框架.遗憾的是, ...

  7. Cocos 更新时反复杀进程,导致差异更新失效的Bug

    Cocos 更新时反复杀进程时,差异更新失效的问题: 问题复现步骤: 1.在project.manifest.temp 文件下载成功后,下载Assets资源的时候杀掉进程 2.重启游戏,继续更新时会使 ...

  8. SQL 性能分析

    SELECT s2.dbid, s1.sql_handle, (SELECT TOP SUBSTRING(s2.text,statement_start_offset / + , ( (CASE WH ...

  9. halcon电路断裂检测

    read_image (Image, 'pcb')dev_close_window ()get_image_size (Image, Width, Height)dev_open_window (0, ...

  10. 系统编码,文件编码,python编码

    系统编码,可以通过locale命令查看(LINUX)https://wiki.archlinux.org/index.php/Locale_(简体中文), centos7 配置文件在/etc/prof ...