题目概述:Fire Net

  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.

  he  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.

Sample  input:

4
.X..
....
XX..
....
2
XX
.X
3
.X.
X.X
.X.
3
...
.XX
.XX
4
....
....
....
....
0

Sample  output:

5
1
5
2
4

简单描述

  题是英文的,重要的语句我已经标出来了,其实题的意思很简单:

  在一个n*n(最大为4*4)的矩形表格中,由你指定在哪些表格不空(用"X"表示,代表wall),哪些表格是空的(用"."表示,可以建blockhouses),现在要在空的(".")表格中写O(建blockhouses),要求就是在水平或者竖直方向上不能有两个O直接或间接相邻,问最多可以写几个O(建blockhouses)?


题目分析

  1、不空的表格由自己决定,即为输入的一部分

  2、在水平或者竖直方向上不能有两个O直接或间接相邻,意味着需要作遍历判断

  3、最多可以写几个O(建blockhouses),意味着需要对所有表格进行分析

  下面贴出源代码,其中我对最主要的代码都作了详细的注释


解题算法

 #include < stdio.h>

 char map[][];

 int best,n;

 int CanPut(int row, int col)

 /*
*检测与前行或者与前列是否存在冲突,即原文中的
*no two blockhouses are on the same horizontal row or vertical column in a map unless there is at least one wall separating them
*如果bullets cannot run through,则返回1
*否则bullets can run through,返回0
*/ {
int i;
for (i = row - ; i >= ; i--)
{
if (map[i][col] == 'O') return ;
if (map[i][col] == 'X') break;
}
for (i = col - ; i >= ; i--)
{
if (map[row][i] == 'O') return ;
if (map[row][i] == 'X') break;
}
return ;
} void solve(int k,int tot)
/*
*calculates the maximum number of blockhouses that can be placed in the city in a legal configuration
*k表示被检测的map单元个数
*tot表示可以放置blockhouses的个数
*/
{
int x,y;
if(k==n*n)//保证整个地图都被检测过
{
if(tot>best)
{
best=tot;
return;
}
}
else
{
x=k/n; //先逐行进行检测
y=k%n; //逐列进行检测
if((map[x][y]=='.') && (CanPut(x,y) ) )//是open space,并且 bullets cannot run through
{
map[x][y]='O';//'0'表示已经检测过并且可放置blockhouses,即将tot+1
solve(k+,tot+);//map[x][y]可以放置blockhouses,则从map[(k+1)/n][(k+1)%n]开始继续检测,即逐行进行检测,并且tot+1
map[x][y]='.';//在恢复堆栈的时候,还原map原来的数据
}
solve(k+,tot);//若map[k/n][k%n]存在bullets can run through,则继续从map[(k+1)/n][(k+1)%n]开始逐行检测
}
}
int main()
{
int i,j;
scanf("%d",&n);
while(n>)
{
for(i=;i< n;i++)
{
for(j=;j< n;j++)
{
scanf("%1s",&map[i][j]);//输入单个字符并且忽略空白
}
}
best=;
solve(,);
printf("%d\n",best);
n=;//预防scanf失败,reset n
scanf("%d",&n);
}
return ;
}

【Acm】算法之美—Fire Net的更多相关文章

  1. 【EatBook】-NO.2.EatBook.2.JavaArchitecture.1.001-《修炼Java开发技术在架构中体验设计模式和算法之美》-

    1.0.0 Summary Tittle:[EatBook]-NO.2.EatBook.2.JavaArchitecture.1.001-<修炼Java开发技术在架构中体验设计模式和算法之美&g ...

  2. ACM,算法

    ACM,算法 描述 最近Topcoder的XD遇到了一个难题,倘若一个数的三次方的后三位是111,他把这样的数称为小光棍数.他已经知道了第一个小光棍数是471,471的三次方是104487111,现在 ...

  3. 推荐学习《算法之美:指导工作与生活的算法》中文PDF+英文PDF

    我们所有人的生活都受到有限空间和有限时间的限制,因此常常面临一系列难以抉择的问题.在一天或者一生的时光里,哪些事是我们应该做的,哪些是应该放弃的?我们对杂乱无序的容忍底线是什么?新的活动与熟悉并喜爱的 ...

  4. JavaScript 数据结构与算法之美 - 线性表(数组、栈、队列、链表)

    前言 基础知识就像是一座大楼的地基,它决定了我们的技术高度. 我们应该多掌握一些可移值的技术或者再过十几年应该都不会过时的技术,数据结构与算法就是其中之一. 栈.队列.链表.堆 是数据结构与算法中的基 ...

  5. JavaScript 数据结构与算法之美 - 十大经典排序算法汇总(图文并茂)

    1. 前言 算法为王. 想学好前端,先练好内功,内功不行,就算招式练的再花哨,终究成不了高手:只有内功深厚者,前端之路才会走得更远. 笔者写的 JavaScript 数据结构与算法之美 系列用的语言是 ...

  6. JavaScript 数据结构与算法之美 - 栈内存与堆内存 、浅拷贝与深拷贝

    前言 想写好前端,先练好内功. 栈内存与堆内存 .浅拷贝与深拷贝,可以说是前端程序员的内功,要知其然,知其所以然. 笔者写的 JavaScript 数据结构与算法之美 系列用的语言是 JavaScri ...

  7. JavaScript 数据结构与算法之美 - 冒泡排序、插入排序、选择排序

    1. 前言 算法为王. 想学好前端,先练好内功,只有内功深厚者,前端之路才会走得更远. 笔者写的 JavaScript 数据结构与算法之美 系列用的语言是 JavaScript ,旨在入门数据结构与算 ...

  8. JavaScript 数据结构与算法之美 - 归并排序、快速排序、希尔排序、堆排序

    1. 前言 算法为王. 想学好前端,先练好内功,只有内功深厚者,前端之路才会走得更远. 笔者写的 JavaScript 数据结构与算法之美 系列用的语言是 JavaScript ,旨在入门数据结构与算 ...

  9. JavaScript 数据结构与算法之美 - 桶排序、计数排序、基数排序

    1. 前言 算法为王. 想学好前端,先练好内功,只有内功深厚者,前端之路才会走得更远. 笔者写的 JavaScript 数据结构与算法之美 系列用的语言是 JavaScript ,旨在入门数据结构与算 ...

随机推荐

  1. Ubuntu下架设FTP服务器(转)

    Ubuntu下架设FTP服务器 Linux下提供了很多的ftp服务器,这里我选用了安全,快速,简单的vsftpd作为FTP服务器.本文是我在自己的Ubuntu 10.10 -32 位系统下搭建的.搭建 ...

  2. linux达人养成计划学习笔记(三)—— 帮助命令

    一.帮助命令man 1.基本使用方法: man 命令 #获取指定命令的帮助选项: -f 查看命令拥有的帮助级别 相当于whatis,也可以使用whereis来查询 -num 调用对应等级的帮助文件 - ...

  3. 安卓listView实现下拉刷新上拉加载滑动仿QQ的删除功能

    大家对这些功能都是看的多了,然后对上拉刷新和下拉加载的原理都是非常清楚的,所以实现这功能其实也就是为了让大家能够从众多的同行们来进行比较学习而已,虽然即使是这样,但是面试的时候面试官还是会问你上拉和下 ...

  4. 【MySQL】MySQL存储过程介绍

    目录结构: contents structure [-] 存储过程简介 关于MySQL的存储过程 MySQL存储过程的创建 格式 声明分割符 参数 变量 注释 MySQL存储过程的调用 MySQL存储 ...

  5. Tensorflow中的run()函数

    1 run()函数存在的意义 run()函数可以让代码变得更加简洁,在搭建神经网络(一)中,经历了数据集准备.前向传播过程设计.损失函数及反向传播过程设计等三个过程,形成计算网络,再通过会话tf.Se ...

  6. docker学习笔记 --- centos install

    Docker简介: Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源. Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后发 ...

  7. 【转】对 Parser 的误解

    一直很了解人们对于parser的误解,可是一直都提不起兴趣来阐述对它的观点.然而我觉得是有必要解释一下这个问题的时候了.我感觉得到大部分人对于parser的误解之深,再不澄清一下,恐怕这些谬误就要写进 ...

  8. linux卸载自带jdk

    centos 6.5系统 java -version: rpm -qa | grep jdk rpm -qa | grep gcj: 使用: yum -y remove java-1.5.0-gcj- ...

  9. SharePoint 2013 启用 查看PDF功能

    SharePoint 2013 默认不能直接Online (注:此Online非OWA概念,而是可以实现直接调用客户端软件实现对文档的编辑,保存之后同步上传)打开PDF(SharePoint 2013 ...

  10. Sublime text —— 自定义主题Soda

    编辑器的主题有两种,一种是语法高亮颜色主题,一种是编辑器自身显示主题,如果要自定义编辑器样式,个人推荐soda. Ctrl+Shift+p 输入install,接着输入  soda,选择  Theme ...