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 ...
随机推荐
- 【转】关于免费SSL证书的那些事儿
根据 Let’s Encrypt CA 的统计,截至 2017 年 11 月,Firefox 加载的网页中启用 HTTPS 的比例占 67%,比去年底的 45% 有巨大提升.浏览器开发商如 Mozil ...
- 【MySQL】MySQL中查询出数据表中存在重复的值list
1.目的:查询MySQL数据表中,重复记录的值 2.示例: 3.代码: select serial_num,count(*) as count FROM card_ticket GROUP BY se ...
- Docker+Nginx+Keepalived实现架构高可用
一.背景 通过keepalived实现nginx高可用,由于在家不想弄多台主机来搞,所以将运行环境用docker封装来模拟跨主机 docker基础镜像:centos 说之前,简单介绍一下: Keepa ...
- 单片机成长之路(51基础篇) - 007 CH340CH341最全说明
比较全的CH340,CH341应用说明 CH340 以前用USB转串口时图方便一般也都是直接买现成的USB转串口模块,但是后面设计需要,不等不将CH340这个模块集成到电路板中,经过多次失败,终于得到 ...
- redis应用-sortedset实现排行榜(转载)
package site.zy9.redisApp.test; import java.util.HashMap; import java.util.List; import java.util.Ma ...
- 很烦人的maven和gradle的jar缓存
1. 起因 a. 最近在学习大数据相关东西,自然就少不免去操作linux系统,更别说不敲命令 b. 然而那个配置软件时,很经常使用ln -s为一个软件目录(带着版本或者其他信息的长命名)创建别名(软连 ...
- vs2013cs页面的代码太长,除了方法,没有折叠,如何处理
VS再带一款插件,工具->扩展和更新,然后选择"联机",在搜索框搜索C# outline 2013,然后安装重启vs即可
- C语言 · 超级玛丽
算法提高 超级玛丽 时间限制:1.0s 内存限制:256.0MB 问题描述 大家都知道"超级玛丽"是一个很善于跳跃的探险家,他的拿手好戏是跳跃,但它一次只能向前跳 ...
- YARN 启动后失败退出——没有请求资源——Invalid resource request, no resources request
在ambari-server中修改了yarn的配置,重新启动服务,结果RM启动失败,错误也很奇怪,“不合理的资源请求,没有请求任何资源”!详细如下: -- ::, FATAL resourcemana ...
- thinkphp3.2在nginx下的配置
最近一直没用nginx 昨天将tp3.2的项目部署到Ubuntu下的nginx下,发现忘记怎么配置的了 特将配置方式记录下来,以方便日后查找 服务器nignx 配置文件 server { listen ...