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. 深入浅出MFC——消息映射与命令传递(六)

    1. 消息分类: 2. 万流归宗——Command Target(CCmdTarget): 3. "消息映射"是MFC内建的一个信息分派机制.通过三个宏(DECLARE_MESSA ...

  2. Python Subprocess Popen 管道阻塞问题分析解决

    http://ju.outofmemory.cn/entry/279026 场景:1>不断播放mp3文件: 2>使用订阅发布模式保持tcp长连接,从服务器接收信息 造成程序hang死,但是 ...

  3. ts+antd报错error TS2605: JSX element type Xxx is not a constructor function for JSX elements

    antd 3 以前的版本需要在 tsconfig.json 的 compilerOptions 中配置 "allowSyntheticDefaultImports": true

  4. Java的Integer和int有什么区别

    Java是面向对象的编程语言,一切都是对象,但是为了编程的方便还是引入了基本数据类型,为了能够将这些基本数据类型当成对象操作,Java为每一个基本数据类型都引入了对应的包装类型(wrapper cla ...

  5. 执行Batch批处理遇到的问题

    1.务必关掉自动提交 增强执行效率 conn.setAutoCommit(false); 2.executeBatch失效问题 <1>务必将语句pstmt = conn.prepareSt ...

  6. Windows驱动开发之线程与同步事件

    转载请注明来源: enjoy5512的博客 : http://blog.csdn.net/enjoy5512 GitHub : https://github.com/whu-enjoy .1. 使用系 ...

  7. JS - 查找同辈中的对象

    今天在使用parent().find(".a:first")的时候,发现查找结果非正常按照顺序来的.有点递归的感觉,从底层往上. 因为需要的是同级的对象,所以去查了一下jquery ...

  8. 分布式实时日志系统(二) 环境搭建之 flume 集群搭建/flume ng资料

    最近公司业务数据量越来越大,以前的基于消息队列的日志系统越来越难以满足目前的业务量,表现为消息积压,日志延迟,日志存储日期过短,所以,我们开始着手要重新设计这块,业界已经有了比较成熟的流程,即基于流式 ...

  9. 问题记录 为ubuntu16.04添加windows字体(解决JIRA图表乱码的问题)

    最近遇到了JIRA在新的ubuntu机器上图表的中文无法正确显示的问题,解决的方法是,为ubuntu安装中文字体,我们选择把windows上的字体复制到ubuntu上来安装的方法,步骤如下: 从win ...

  10. virgo-tomcat-server最大并发连接数的修改

    首先,我们如果需要修改tomcat 7的最大连接数,我们可以去tomcat官方网站,查看Documentation 进入tomcat的官方网站http://tomcat.apache.org我们点击左 ...