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. Mybatis输入输出映射

    一.输入映射 1.传递简单类型 <select id="findUserById" parameterType="int" resultType=&quo ...

  2. openldap slapd.conf参数

    已安装系统的 /etc/openldap/slapd.conf 中包含 LDAP 服务器的完整配置文件.在此简述了其中的各个项并说明了必要的调整.以符号 (#) 为前缀的项处于非活动状态.必须取消这个 ...

  3. Stratix内嵌存储器测试报告

         Stratix和Stratix GX系列器件内嵌TriMatrix存储块包括512-bit M512块.4-Kbit M4K块及512-Kbit M-RAM块.TriMatrix存储结构可对 ...

  4. python爬虫---->scrapy的使用(一)

    这里我们介绍一下python的分布式爬虫框架scrapy的安装以及使用.平庸这东西犹如白衬衣上的污痕,一旦染上便永远洗不掉,无可挽回. scrapy的安装使用 我的电脑环境是win10,64位的.py ...

  5. jQuery属性操作(二)

    挂载到$上的几个属性操作方法分析,发现属性操作用到了sizzle封装的方法 attr: function( elem, name, value ) {        var hooks, ret,   ...

  6. laravel blade模板里调用路由方法重定向

    @if (Session::get('user') == NULL) {!!Redirect::to('login')!!} @endif or @if (Session::get('user') = ...

  7. 在openLdap上添加memberOf属性

    我为openldap添加memberof属性的时候参考了这个文章:http://www.adimian.com/blog/2014/10/how-to-enable-memberof-using-op ...

  8. MacOS 安装 nginx

    brew install nginx 开机启动 $ sudo cp `brew --prefix nginx`/homebrew.mxcl.nginx.plist /Library/LaunchDae ...

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

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

  10. Java内存泄露监控工具:JVM监控工具介绍【转】

    jstack?-- 如果java程序崩溃生成core文件,jstack工具可以用来获得core文件的java stack和native stack的信息,从而可以轻松地知道java程序是如何崩溃和在程 ...