blockhouses

时间限制:1000 ms  |  内存限制:65535 KB
难度:3
描写叙述
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】的更多相关文章

  1. NYOJ 722 数独 【DFS】+【预处理】

    数独 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描写叙述 数独是一种运用纸.笔进行演算的逻辑游戏.玩家须要依据9×9盘面上的已知数字,推理出全部剩余空格的数字,并满足每一 ...

  2. 【第40套模拟题】【noip2011_mayan】解题报告【map】【数论】【dfs】

    目录:1.潜伏者 [map] 2.Hankson的趣味题[数论]3.mayan游戏[dfs] 题目: 1. 潜伏者(spy.pas/c/cpp)[问题描述]R 国和S 国正陷入战火之中,双方都互派间谍 ...

  3. Kattis - glitchbot 【DFS】

    Kattis - glitchbot [DFS] 题意 有一个机器人 刚开始在(0, 0),然后给出一个目标点,并且会给出一系列指令,但是其中会有一个指令是错误的.我们需要找出那个指令,并且改成正确的 ...

  4. HDU 6113 度度熊的01世界 【DFS】(2017"百度之星"程序设计大赛 - 初赛(A))

    度度熊的01世界 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  5. 【dfs】P1331 海战

    题目描述 在峰会期间,武装部队得处于高度戒备.警察将监视每一条大街,军队将保卫建筑物,领空将布满了F-2003飞机.此外,巡洋船只和舰队将被派去保护海岸线.不幸的是因为种种原因,国防海军部仅有很少的几 ...

  6. 【dfs】p1731 生日蛋糕

    1441:[例题2]生日蛋搞 [题目描述] 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体.设从下往上数第i(1≤i≤M)层蛋糕是半径为Ri, 高 ...

  7. 【dfs】LETTERS

    1212:LETTERS [题目描述] 给出一个roe×colroe×col的大写字母矩阵,一开始的位置为左上角,你可以向上下左右四个方向移动,并且不能移向曾经经过的字母.问最多可以经过几个字母. [ ...

  8. 洛谷P1605 迷宫【dfs】

    题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫 中移动有上下 ...

  9. 【dfs】BZOJ1703-[Usaco2007 Mar]Ranking the Cows 奶牛排名

    [题目大意] 农夫约翰有N(1≤N≤1000)头奶牛,每一头奶牛都有一个确定的独一无二的正整数产奶率.约翰想要让这些奶牛按产奶率从高到低排序,约翰已经比较了M(1≤M≤10000)对奶牛的产奶率,但他 ...

随机推荐

  1. 基础知识《零》---一张图读懂JDK,JRE,JVM的区别与联系

  2. mysqlint类型的长度值mysql在建表的时候int类型后的长度代表什么

    详解mysql int类型的长度值 mysql在建表的时候int类型后的长度代表什么 是该列允许存储值的最大宽度吗 为什么我设置成int(1), 也一样能存10,100,1000呢. 当时我虽然知道i ...

  3. 【宝塔面板】pm2 安装没反应问题

    在使用宝塔安装 node环境时候..点击 PM2管理器安装.然后没反应.刷新列表.还是未安装 解决办法: 进入SSH, 查看错误 cat /tmp/panelExec.log 更新git yum up ...

  4. Express+Less+Gulp配置高效率开发环境

    版权声明:本文由金朝麟原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/627049001486519548 来源:腾云阁 h ...

  5. Android Studio 3.0.1 版本包下载

    Android Studio 3.0.1 发布了,这是对 Android Studio 3.0 的一个小的更新,包括一般错误修复和性能改进 下载地址: Windows 64 位:https://dl. ...

  6. GTID与MHA

    MHA 基于binlog文件位置的复制 * Phase 3: Master Recovery Phase.. * Phase 3.1: Getting Latest Slaves Phase.. La ...

  7. vue Element-UI组件

    一.UI组件 目的: 提高开发效率, 别人提供好一切, 拿过来直接用饿了么团队开源一个基于vue组件库 Element-UI ==> pc端 文档: http://element-cn.elem ...

  8. facebook login issue

    If enable the facebook account in settings, when change account can't open the session. -(void)fbRes ...

  9. 微信小游戏 交互接口的使用 wx.showToast wx.showLoading

    在小游戏中,会有如下图的提示窗口,这些可以使用微信提供的交互接口实现. 使用loading等待的接口.mask=true表示遮罩,防止等待时点击其他按钮触发其他操作导致异常. wx.showLoadi ...

  10. iOS计算两个时间的时间差

    + (long)calculteHourL:(NSDate *)endDate startDate:(NSDate *)startDate { NSCalendar *cal = [NSCalenda ...