zoukankan      html  css  js  c++  java
  • UVA Don't Get Rooked

    主题如以下:


     Don't Get Rooked 

    In chess, the rook is a piece that can move any number of squaresvertically or horizontally. In this problem we will consider smallchess boards (at most 4$	imes$4) that can also contain walls through whichrooks cannot move. The goal is to place as many rooks on a board aspossible so that no two can capture each other. A configuration ofrooks is legal provided that no two rooks are on the samehorizontal row or vertical column unless there is at least one wallseparating them.


    The following image shows five pictures of the same board. Thefirst picture is the empty board, the second and third pictures show legalconfigurations, and the fourth and fifth pictures show illegal configurations.For this board, the maximum number of rooks in a legal configurationis 5; the second picture shows one way to do it, but there are severalother ways.

    Your task is to write a program that, given a description of a board,calculates the maximum number of rooks that can be placed on theboard in a legal configuration.

    Input 

    The input file contains one or more board descriptions, followed bya line containing the number 0 that signals the end of the file. Eachboard description begins with a line containing a positive integer nthat is the size of the board; n will be at most 4. The next nlines each describe one row of the board, with a `.' indicating anopen space and an uppercase `X' indicating a wall. There are nospaces in the input file.

    Output 

    For each test case, output one line containing themaximum number of rooks that can be placed on the boardin 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
    

    跟八皇后问题差点儿相同,就是多了墙而已,这样不一定每行每列仅仅有一个点,中间可能隔着墙。所以用一个函数推断两点之间是否有墙。

    用一个函数推断一个点能否被放置,可以的条件是它与所在行所在列的不论什么一个已经放置的点之间都有墙。有了这个函数后就行对每一个点展开DFS,更新最大值。

    AC的代码例如以下:


    版权声明:本文博客原创文章。博客,未经同意,不得转载。

  • 相关阅读:
    Solace 简介
    Google BERT摘要
    Difference between java.lang.RuntimeException and java.lang.Exception
    第三方deepvoice3_pytorch安装使用
    通过setup.py安装项目dependencies
    Heroku 教程
    使用谷歌CoLaboratory训练神经网络
    最完整的自动化测试流程:Python编写执行测试用例及定时自动发送最新测试报告邮件
    Jmeter参数化之数据库读取数据
    python之lambda、filter、map、reduce的用法说明(基于python2)
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4734544.html
Copyright © 2011-2022 走看看