NYOJ 587 blockhouses 【DFS】
blockhouses
- 描写叙述
- 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.
- 输入
- 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. - 输出
- For each test case, output one line containing the 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 - 例子输出
-
5
1
5
2
4
#include <stdio.h>
char map[6][6];
int vis[6][6];
int n, max, count;
void getFireRange(int row, int col){
int i = 1;
while(row + i < n){ //向下开火
if(map[row+i][col] == 'X') break;
++vis[row+i][col]; ++i;
}
i = 1;
while(row - i >= 0){
if(map[row-i][col] == 'X') break;
++vis[row-i][col]; ++i;
}
i = 1;
while(col - i >= 0){
if(map[row][col-i] == 'X') break;
++vis[row][col-i]; ++i;
}
i = 1;
while(col + i < n){
if(map[row][col+i] == 'X') break;
++vis[row][col+i]; ++i;
}
}
void cancelFireRange(int row, int col){
int i = 1;
while(row + i < n){ //向下开火
if(map[row+i][col] == 'X') break;
--vis[row+i][col]; ++i;
}
i = 1;
while(row - i >= 0){
if(map[row-i][col] == 'X') break;
--vis[row-i][col]; ++i;
}
i = 1;
while(col - i >= 0){
if(map[row][col-i] == 'X') break;
--vis[row][col-i]; ++i;
}
i = 1;
while(col + i < n){
if(map[row][col+i] == 'X') break;
--vis[row][col+i]; ++i;
}
}
bool check(int i, int j){
if(map[i][j] == 'X' || vis[i][j]) return 0;//火力覆盖或者已有碉堡
return 1;
}
void DFS(int row, int col){
for( ; row < n; ++row, col = 0){
for( ; col < n; ++col){
if(map[row][col] == '.' && check(row, col)){
vis[row][col] = 1; ++count;
if(count > max) max = count;
getFireRange(row, col);
DFS(row, col + 1);
cancelFireRange(row, col);
vis[row][col] = 0; --count;
}
}
}
}
int main(){
while(scanf("%d", &n), n){
for(int i = 0; i < n; ++i){
getchar();
for(int j = 0; j < n; ++j){
map[i][j] = getchar();
vis[i][j] = 0;
}
}
max = count = 0;
DFS(0, 0); //从第0行第0列開始
printf("%d\n", max);
}
return 0;
}
NYOJ 587 blockhouses 【DFS】的更多相关文章
- NYOJ 722 数独 【DFS】+【预处理】
数独 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描写叙述 数独是一种运用纸.笔进行演算的逻辑游戏.玩家须要依据9×9盘面上的已知数字,推理出全部剩余空格的数字,并满足每一 ...
- 【第40套模拟题】【noip2011_mayan】解题报告【map】【数论】【dfs】
目录:1.潜伏者 [map] 2.Hankson的趣味题[数论]3.mayan游戏[dfs] 题目: 1. 潜伏者(spy.pas/c/cpp)[问题描述]R 国和S 国正陷入战火之中,双方都互派间谍 ...
- Kattis - glitchbot 【DFS】
Kattis - glitchbot [DFS] 题意 有一个机器人 刚开始在(0, 0),然后给出一个目标点,并且会给出一系列指令,但是其中会有一个指令是错误的.我们需要找出那个指令,并且改成正确的 ...
- HDU 6113 度度熊的01世界 【DFS】(2017"百度之星"程序设计大赛 - 初赛(A))
度度熊的01世界 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- 【dfs】P1331 海战
题目描述 在峰会期间,武装部队得处于高度戒备.警察将监视每一条大街,军队将保卫建筑物,领空将布满了F-2003飞机.此外,巡洋船只和舰队将被派去保护海岸线.不幸的是因为种种原因,国防海军部仅有很少的几 ...
- 【dfs】p1731 生日蛋糕
1441:[例题2]生日蛋搞 [题目描述] 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体.设从下往上数第i(1≤i≤M)层蛋糕是半径为Ri, 高 ...
- 【dfs】LETTERS
1212:LETTERS [题目描述] 给出一个roe×colroe×col的大写字母矩阵,一开始的位置为左上角,你可以向上下左右四个方向移动,并且不能移向曾经经过的字母.问最多可以经过几个字母. [ ...
- 洛谷P1605 迷宫【dfs】
题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫 中移动有上下 ...
- 【dfs】BZOJ1703-[Usaco2007 Mar]Ranking the Cows 奶牛排名
[题目大意] 农夫约翰有N(1≤N≤1000)头奶牛,每一头奶牛都有一个确定的独一无二的正整数产奶率.约翰想要让这些奶牛按产奶率从高到低排序,约翰已经比较了M(1≤M≤10000)对奶牛的产奶率,但他 ...
随机推荐
- tp3.2 ajax 表单提交
前台: 1 <form action="javascript:;" method="post" class="form_div" id ...
- WP8.1学习系列(第二十二章)——在页面之间导航
在本文中 先决条件 创建导航应用 Frame 和 Page 类 页面模板中的导航支持 在页面之间传递信息 缓存页面 摘要 后续步骤 相关主题 重要的 API Page Frame Navigation ...
- 通过orderby关键字,LINQ可以实现升序和降序排序。LINQ还支持次要排序。
通过orderby关键字,LINQ可以实现升序和降序排序.LINQ还支持次要排序. LINQ默认的排序是升序排序,如果你想使用降序排序,就要使用descending关键字. static void M ...
- 解决Unknown error: Unable to build: the file dx.jar was not loaded from the SDK folder!
解决Unknown error: to the dx.jar the SDK folder!最近渐渐迁移到Android Studio来了,更新过Android SDK Manager里的东西后,打开 ...
- Free Download Manager (FDM) 中文版 - 替代迅雷最佳免费开源下载工具软件
https://www.freedownloadmanager.org/ Free Download Manager (FDM) 是一款经典免费纯粹的下载软件,它开源无广告,界面简洁清爽,支持 BT. ...
- slideout
这里在原有slideout.js增加了shade的遮罩功能 核心: 1,此插件的使用需要配合dom来用: <!-- 左边 --> <nav id="menu" c ...
- JVM垃圾收集器组合--各种组合对应的虚拟机参数实践
前言 相信很多人都看过下面这张图,(来自<深入理解Java虚拟机:JVM高级特性与最佳实践>) 在学完几种垃圾收集器类型及组合后,打算看看实际中程序用到的垃圾收集器. 但是在jconsol ...
- 【转】Windows socket基础
转自:http://blog.csdn.net/ithzhang/article/details/8448655 Windows socket 基础 Windows socket是一套在Windows ...
- 【JSP】JSP中的Java脚本
前言 现代Web开发中,在JSP中嵌入Java脚本不是推荐的做法,因为这样 不利于代码的维护.有很多好的,替代的方法避免在JSP中写Java脚本.本文仅做为JSP体系技术的一个了解. 类成员定 ...
- 添加Net4CollectionTypeFactory的原因
.NET4.0已经实现了该功能 http://jahav.com/blog/nhibernate-using-net-4-iset/ NHibernate using .NET 4 ISet 0 Co ...