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. 写GULP遇到的ES6问题详解

    Gulp.js 是一个自动化构建工具,开发者可以使用它在项目开发过程中自动执行常见任务.最近复习一下gulp一些基本的写法,在写了一些简单的uglify,rename,concat,clean的处理之 ...

  2. querySelector与getElementBy系列的区别

    getElementBy系列 document.getElementsByTagName('tag'); document.getElementById('id'); document.getElem ...

  3. 【心有猛虎】react-pxq

    这是一个比较完整的简单的react移动端项目,说起来页面少,其实,构思若是精巧,也并不容易做 先放源码:https://github.com/bailicangdu/react-pxq 接下来我们简单 ...

  4. R语言处理Web数据

    R语言处理Web数据 许多网站提供的数据,以供其用户的消费.例如,世界卫生组织(WHO)提供的CSV,TXT和XML文件的形式的健康和医疗信息报告.基于R程序,我们可以通过编程提取这些网站的具体数据. ...

  5. Directx11教程(64) tessellation学习(6)-PN Triangles

    原文:Directx11教程(64) tessellation学习(6)-PN Triangles       前面我们用tessellation细分三角形或者四边形,产生的细分点都是在三角形或四边形 ...

  6. iOS app发布流程

    http://www.xuebuyuan.com/1980497.html http://blog.csdn.net/alincexiaohao/article/details/38725367 ap ...

  7. MVC设计之从零打造到实际操作(总汇)

    我们为什么要自己搭建一个MVC架构的框架? 1.为了更快的开发效率 2.为了更高的运行效率 3.为了更好的证明自己 在别的框架中,有些方法我们使用起来可能会比较麻烦,我们可以在自己的框架中写一些自己想 ...

  8. 【JZOJ4819】【NOIP2016提高A组模拟10.15】算循环

    题目描述 输入 输出 样例输入 167 198 样例输出 906462341 数据范围 解法 令f(n)=∑ni=1i,g(n)=∑ni=1i2 易得ans=∑ni=1∑mj=1f(n−i+1)∗f( ...

  9. HDU-1260_Tickets

    Tickets Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem Des ...

  10. 《C语言深度解剖》学习笔记之指针和数组

    第4章 指针和数组 1. int *p=NULL 和 *p=NULL 有什么区别 int *p = NULL; 第一句代码的意思是:定义一个指针变量p,其指向的内存里面保存的是 int类型的数据:在定 ...