hdu 1045 Fire Net(dfs)
Fire Net
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8239 Accepted Submission(s):
4734
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.
4
.X..
....
XX..
....
2
XX
.X
3
.X.
X.X
.X.
3
...
.XX
.XX
4
....
....
....
....
0
#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)的更多相关文章
- HDU 1045 Fire Net(dfs,跟8皇后问题很相似)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 1045 Fire Net(DFS 与8皇后问题类似)
Fire Net Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- hdu 1045:Fire Net(DFS经典题)
Fire Net Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- 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(二分图)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1045 题目大意为给定一个最大为4*4的棋盘,棋盘可以放置堡垒,处在同一行或者同一列的堡垒可以相互攻击, ...
- HDU 1045 - Fire Net (最大独立集)
题意:给你一个正方形棋盘.每个棋子可以直线攻击,除非隔着石头.现在要求所有棋子都不互相攻击,问最多可以放多少个棋子. 这个题可以用搜索来做.每个棋子考虑放与不放两种情况,然后再判断是否能互相攻击来剪枝 ...
- HDU 1045 Fire Net(搜索剪枝)
http://acm.split.hdu.edu.cn/showproblem.php?pid=1045 http://acm.hdu.edu.cn/showproblem.php?pid=1045 ...
- HDU1045 Fire Net(DFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others) ...
- 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 ...
随机推荐
- Django快速创建新项目
Python免费视频含配套文件QQ124111294 https://pan.baidu.com/s/1bL5ml4 python.exe manage.py startapp app01 pytho ...
- CSS3渐变效果
一.线性渐变linear-gradient 1.使用方法: background:-webkit-linear-gradient(red,blue);background:-moz-linear-g ...
- 在redhat 6.6上安装Docker
安装环境 支持Docker的RHEL版本 Red Hat Enterprise Linux 7 (64-bit) Red Hat Enterprise Linux 6.5 (64-bit) 或更高版本 ...
- vs code 配置c/c++环境
1. 编译 通过 code-runner插件 运行编译 安装code-runner后在settings.json中找到code-runner.executorMap,可以看到其中的cpp 文件运行方式 ...
- eNSP模拟器路由器无法正常启动一直显示“#”——问题解决方法
eNSP模拟器路由器无法正常启动一直显示“#” 问题项如截图: 解决方案: 1. 打开自己电脑的控制面板 -->> 系统和安全 -->> Windows Defender防火墙 ...
- ThInkPHP加密和解密cookie(登录操作)
摘自:http://www.thinkphp.cn/code/1794.html 通过加密cookie是网站安全性更高,登录信息不保存在session中在function.php文件在建立两个函数,加 ...
- spark-ML基础
一.ML组件 ML的标准API使用管道(pipeline)这样的方式,可以将多个算法或者数据处理过程整合到一个管道或者一个流程里运行,其中包含下面几个部分: 1. dataFrame:用于ML的dat ...
- win10上修改docker的镜像文件存储位置
记住:修改的是docker从服务器上拉下来的镜像文件存储位置(本地),是不是镜像源地址(服务器) 首先 win10下的docker有可视化操作界面和命令行操作,下载了docker-ce.exe双击后就 ...
- 2019-10-21-WPF-多个-StylusPlugIn-的事件触发顺序
title author date CreateTime categories WPF 多个 StylusPlugIn 的事件触发顺序 lindexi 2019-10-21 08:33:15 +080 ...
- CSS Specificity
CSS的specificity特性或称非凡性,它是一个衡量CSS值优先级的一个标准. 其实就是解决冲突,当同一个元素被CSS选择符选中,按照优先权取舍不同的CSS规则. specificity用一个四 ...