Fire Net

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8239    Accepted Submission(s):
4734

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:  1051 1067 1258 1053 1052 
 
一道搜索题,因为数据很小,最大为4,所以可以直接深搜枚举,还是很不习惯写深搜,想了很久,最后参照别人的代码写的。
 
题意:与八皇后类似,炮台不能存在于同行同列,除非中间隔墙。
 
 #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int n,s;
char map[][];
int can(int x,int y)
{
int i;
for(i=x-; i>=; i--)
{
if(map[i][y]=='X') //若出现墙,则可以直接跳出
break;
else if(map[i][y]=='K')
return ;
}
for(i=y-; i>=; i--)
{
if(map[x][i]=='X')
break;
else if(map[x][i]=='K')
return ;
}
return ;
}
void DFS(int y,int count)
{
int i,j;
if(count>s) //某一种方法的最大炮台数赋给s保存
s=count;
for(i=y; i<n; i++) //每一行都要搜索
for(j=; j<n; j++)
{
if(map[i][j]=='.'&&can(i,j)) //can函数判断这个炮台是否能存在
{
map[i][j]='K'; //放了炮台的标记为K
count++;
DFS(i,count); //进入下一次循环
count=;
map[i][j]='.';
}
}
}
int main()
{
while(~scanf("%d",&n)&&n)
{
for(int i=; i<n; i++)
scanf("%s",map[i]); //输入地图
s=;
DFS(,); //第0行开始搜,炮台数量s初始化为0
printf("%d\n",s);
}
return ;
}

hdu 1045 Fire Net(dfs)的更多相关文章

  1. HDU 1045 Fire Net(dfs,跟8皇后问题很相似)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others)   ...

  2. HDU 1045 Fire Net(DFS 与8皇后问题类似)

    Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  3. hdu 1045:Fire Net(DFS经典题)

    Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  4. HDU - 1045 Fire Net(搜索)

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

  5. hdu 1045 Fire Net(二分图)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1045 题目大意为给定一个最大为4*4的棋盘,棋盘可以放置堡垒,处在同一行或者同一列的堡垒可以相互攻击, ...

  6. HDU 1045 - Fire Net (最大独立集)

    题意:给你一个正方形棋盘.每个棋子可以直线攻击,除非隔着石头.现在要求所有棋子都不互相攻击,问最多可以放多少个棋子. 这个题可以用搜索来做.每个棋子考虑放与不放两种情况,然后再判断是否能互相攻击来剪枝 ...

  7. HDU 1045 Fire Net(搜索剪枝)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=1045 http://acm.hdu.edu.cn/showproblem.php?pid=1045 ...

  8. HDU1045 Fire Net(DFS)

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

  9. 2017ACM暑期多校联合训练 - Team 1 1003 HDU 6035 Colorful Tree (dfs)

    题目链接 Problem Description There is a tree with n nodes, each of which has a type of color represented ...

随机推荐

  1. 【CS Round #44 (Div. 2 only) C】Check DFS

    [链接]点击打开链接 [题意] 给你一个n节点,m条边的无向联通图. 给你一个节点访问的顺序.(1..n的排列) 你可以改变每个点优先访问的出度.(但必须按照dfs的规则); 问你能不能按照所给的访问 ...

  2. hihocoder1317 :搜索四·跳舞链

    精确覆盖问题是指对于给定的一个由0-1组成的矩阵,是否能找到一个行的集合,使得集合中每一列都恰好包含一个1. //Achen #include<algorithm> #include< ...

  3. QT生成GUID

    #include <QCoreApplication> #include <QUuid> #include <QDebug> int main(int argc, ...

  4. django 验证码(django-simple-captcha)

    django 验证码(django-simple-captcha) django-simple-captcha 官方文档(含基于modelForm的用法)  https://django-simple ...

  5. 2018-9-30-C#-winforms-输入颜色转换颜色名

    title author date CreateTime categories C# winforms 输入颜色转换颜色名 lindexi 2018-09-30 18:27:49 +0800 2018 ...

  6. 网页性能测试之WebPageTest

    想知道您的网站,性能怎么样? 很自然,首先得找一个广被认可的测试工具.我们推荐WebPageTest: WebPageTest 它是google 开源项目”make the web faster “的 ...

  7. Java版阿里云通信短信发送API接口实例(新)

    阿里云通信(原名阿里大于)的短信服务(Short Message Service)是阿里云为用户提供的一种通信服务的能力,支持快速发送短信验证码.短信通知等. 完美支撑双11期间2亿用户,发送6亿短信 ...

  8. jQuery动态加载动画spin.js

    在线演示 本地下载

  9. 2019-8-31-C#-标准性能测试高级用法

    title author date CreateTime categories C# 标准性能测试高级用法 lindexi 2019-08-31 16:55:58 +0800 2018-07-08 0 ...

  10. 阿里巴巴资深技术专家无相:我们能从 InteliJ IDEA 中学到什么?

    本文来源于阿里巴巴资深技术专家无相在内网的分享,阿里巴巴中间件受权发布. 最近因为工作的关系,要将 Eclipse 的插件升级为 IDEA 插件.升级过程中,对 IDEA 插件做了些学习和研究,希望通 ...