HDU - 1045 Fire Net 【DFS】_牧心.的博客-程序员宅基地

技术标签: ACM练习  

Description

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.

Input

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.

Output

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

 

#include <cstdio>
#include <cstring>
using namespace std;

int map[6][6], n, sum;

//判断该位置能否放置碉堡
bool judge(int x, int y)
{
    if (map[x][y])
    {
        return 0;
    }
    if (x > 0)//up
    {
        for (int i = x-1; i >= 1; i--)
        {
            if (map[i][y] == 2)
                break;
            else if (map[i][y] == 1)
                return 0;
        }
    }
    if (x < n)//down
    {
        for (int i = x+1; i <= n; i++)
        {
            if (map[i][y] == 2)
                break;
            else if (map[i][y] == 1)
                return 0;
        }
    }
    if (y > 0)//left
    {
        for (int i = y-1; i >= 1; i--)
        {
            if (map[x][i] == 2)
                break;
            else if (map[x][i] == 1)
                return 0;
        }
    }
    if (y < n)//right
    {
        for (int i = y+1; i <= n; i++)
        {
            if (map[x][i] == 2)
                break;
            else if (map[x][i] == 1)
                return 0;
        }
    }
    return 1;
}

void dfs(int m)//用 m 记录搜索深度
{
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            if (judge(i, j))//如果可以放置碉堡,则进入
            {
                map[i][j] = 1;//标记为已放置
                dfs(m+1);//进入下一层搜索
                map[i][j] = 0;//回溯
            }
        }
    }
    sum = m>sum?m:sum;
}

int main()
{
    char ch;
    while(scanf("%d", &n))
    {
        getchar();
        if (n == 0)
        {
            break;
        }
        memset(map, 0, sizeof(map));
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                scanf("%c", &ch);
                if (ch == 'X')//设置墙的位置
                {
                    map[i][j] = 2;
                }
            }
            if (i < n)
            {
                getchar();
            }
        }

        sum = 0;
        dfs(0);

        printf("%d\n", sum);
    }
    return 0;
}

 

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Aibiabcheng/article/details/105282663

智能推荐

JS读书心得:《JavaScript框架设计》——第12章 异步处理-程序员宅基地

一、何为异步                             执行任务的过程可以被分为发起和执行两个部分。 同步执行模式:任务发起后必须等待直到任务执行完成并返回结果后,才会执行下一个任务。 异步执行模式:任务发起后不等待任务执行完成,而是马上执行下一个任务,当任务执行完成时则会收到通知。 面对IO操作频繁的场景,异步执...

seo中的竞价排名是什么-程序员宅基地

seo中的竞价排名是什么一、总结一句话总结:竞价排名的基本特点是按点击付费,推广信息出现在搜索结果中(一般是靠前的位置),如果没有被用户点击,则不收取推广费。搜索引擎的一种推广广告的方式1、竞价排名优点?见效快关键词数量无限制 关键词不分难易程度1、见效快:充值后设置关键词价格后即刻就可以进入。2、关键词数量无限制:可以在后台设置无数的关键词进行推广,数量自己控...

4-16 递归求简单交错幂级数的部分和 (10分)-程序员宅基地

本题要求实现一个函数,计算下列简单交错幂级数的部分和:f(x,n)=x−x2+x3−x4+⋯+(−1)n−1xn f(x, n) = x - x^2 + x^3 - x^4 + \cdots + (-1)^{n-1}x^nf(x,n)=x−x​2​​+x​3​​−x​4​​+⋯+(−1)​n−1​​x​n​​函数接口定义:double fn( double x, int n

技嘉GA-M55SLI-S4主板北桥风扇问题-程序员宅基地

提示: 流水账+非技术贴,赶时间的朋友请跳过。 最近一段时间以来,家里的PC(平时也就是给老爸老妈玩玩游戏看看股票什么的)开机状态下声音狂响,运行一些高CPU消耗的程序时有死机(直接跳掉关机)。实在不胜其扰,今天终于下定决心给它来个体检。根据鄙人并不丰富的经验,噪音肯定是来自风扇,而时有发生的自动关机现象,怀疑是风扇问题引发的散热不足导致CPU或者其他核心组件过热。观察下来C..._北桥散热风扇线短

Win10 - 对系统进行优化(低配电脑显著提升性能_低配win10优化-程序员宅基地

写在前面最近一直在用笔记本,有点卡,特别是开机后的一段时间内,卡到无法呼吸,所以觉得花点时间好好优化下。如何优化尽量减少不必要的磁盘IO,特别是机械硬盘减少开机启动应用:启动项越多会导致越多的程序提前加载到内存,中间需要走磁盘的IO关闭不使用/不常用的服务:与减少开机启动应用类似,有的服务还会一直占用资源关闭应用程序的自动更新优化:减少开机启动应用在Win10上操作很简单..._低配win10优化

Linux中增加磁盘并进行系统盘数据迁移-程序员宅基地

通常在项目正式上线后,随着Linux服务器中系统盘数据量的不断增长,导致Disk过高,数据存储空间短缺。因此,我们通常需要将系统盘数据进行迁移,通过创建数据盘分区,将系统盘数据进行迁移。接下来对迁移步骤进行一一阐述:一、创建分区1、查看数据盘是否已经分区命令:fdisk -l可看出,本机服务器中有一块磁盘 /dev/sda ,大小为53.7GB,并进行分区的数据盘有两块:/dev/sda1,/dev/sda22、现在对该数据盘进行分区命令:fdisk /dev/sda根据提示,

随便推点

sql 中 mod()的基础用法_sql中mod函数的使用方法-程序员宅基地

Mod(a,b) 在sql中的意思是 a / b的余数基础用法:如果id需要是偶数或者奇数时就可以使用mod。mod(id,2)=1 是指id是奇数。mod(id,2)=0 是指id是偶数。..._sql中mod函数的使用方法

Hive建表时,使用Array和Map类型以及数据导入_hive 建表array-程序员宅基地

在Hive建表时,我们是可以指定数据类型为Array和Map类型的。除此之外还有Struct类型,这里就不对此做过多延伸。参考:Hive增删改查建表:CREATE TABLE test001( id STRING COMMENT '', address ARRAY<string> COMMENT '', jobs map<string,string>);如果是从本地加载文件,我们可以把建表语句改成:CREATE TABLE test001( id STRIN_hive 建表array

教你如何制作vue+springboot项目-程序员宅基地

以本人制作的学生成绩管理系统的基础上,从头到尾教大家如何构建vue+springboot项目_vue+springboot

jmeter 压测 RabbitMQ_单机_jmeter压测rabbitmq-程序员宅基地

文章目录一、MQ压测1. 资料列表2. jmeter软件包3. 插件列表二、远程服务器监控2.1. 监控声明2.2. 监控场景的区别2.3. 软件列表2.4. 插件操作2.5. 软件操作三、jmeter编写MQ脚本一、MQ压测1. 资料列表RabiitMQ 使用Jmeter 进行性能测试,需要准备一下1个软件2插件2. jmeter软件包apache-jmeter-5.1.1.zip3. 插件列表主要插件介绍:MQ压测插件:amqp-client-5.2.0.jarApacheJM_jmeter压测rabbitmq

视频教程-SpringBoot2.X版本优惠券实战整合Dubbo+Rocketmq+Redis-其他-程序员宅基地

SpringBoot2.X版本优惠券实战整合Dubbo+Rocketmq+Redis ..._springboot2.x+dubbo微服务优惠券实战|

串口异步收发的实现_c重叠异步串口-程序员宅基地

实习时做的串口异步控制履带电机的程序_c重叠异步串口