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)对奶牛的产奶率,但他 ...
随机推荐
- 《Lua程序设计》第4章 语句 学习笔记
Lua中的常规语句包括:赋值.控制结构和过程调用.Lua还支持一些不太常见的语句,如:多重赋值(multiple assignment) 和 局部变量声明.4.1 赋值Lua允许“多重赋值”,也就是一 ...
- iOS AOP编程思想及实践
什么是 AOP Wikipedia 上的 AOP 定义: In computing, aspect-oriented programming (AOP) is a programming paradi ...
- IDEA试用期结束激活问题
1.试用期结束,出现IDEA License Activation界面 2.IntelliJ Idea 2017 免费激活方法 方法1. 到网站 http://idea.lanyus.com/ 获取注 ...
- session超时跃出iframe并跳到登陆页面(转载)
session超时跳出iframe并跳到登陆页面 在网页编程时,我们经常需要处理,当session过期时,我们要跳到登陆页面让用户登陆,由于我们可能用到IFrame框架,所以我们我登陆页面需要显示在整 ...
- sass - 公用方法封装
// 设置宽高 @mixin wh($wid,$hei){ @if $wid { width: $wid; } @if $hei { height: $hei; } overflow: hidden; ...
- css3整理--box-shadow
box-shadow语法:(想法:用阴影来做边框,就不会出现动态改变边框宽度而产生的重排问题) 对象选择器{box-shadow:投影方式 X轴偏移量 Y轴偏移量 阴影模糊半径 阴影扩展半径 阴影颜色 ...
- jQuery的回调管理机制
// 对option的一个缓存,避免每次都需要createOptions,option是创建Callback对象时的传入的参数// 每个option被存入optionsCache中类似于{memory ...
- storm事务
1. storm 事务 对于容错机制,Storm通过一个系统级别的组件acker,结合xor校验机制判断一个msg是否发送成功,进而spout可以重发该msg,保证一个msg在出错的情况下至少被重发一 ...
- Elasticsearch学习之嵌套聚合,下钻分析,聚合分析
1. 计算每个tag下的商品数量 GET /ecommerce/product/_search { "aggs": { "group_by_tags": { & ...
- git 推送出现 "fatal: The remote end hung up unexpectedly"
原因:原因是推送的文件太大 解决方案: 注意,有时候会看不到.git文件,可能被隐藏了,在这里勾选上隐藏的项目,就可以看到了. 第一种,全局设置 在C:\Users\wang\git\.git\con ...