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 44)
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的代码例如以下:
版权声明:本文博客原创文章。博客,未经同意,不得转载。
UVA Don't Get Rooked的更多相关文章
- uva 639 Don't Get Rooked 变形N皇后问题 暴力回溯
题目:跟N皇后问题一样,不考虑对角冲突,但考虑墙的存在,只要中间有墙就不会冲突. N皇后一行只能放一个,而这题不行,所以用全图暴力放棋,回溯dfs即可,题目最多就到4*4,范围很小. 刚开始考虑放一个 ...
- Uva 10815-Andy's First Dictionary(串)
Problem B: Andy's First Dictionary Time limit: 3 seconds Andy, 8, has a dream - he wants to produce ...
- UVA 816 - Abbott's Revenge(BFS)
UVA 816 - Abbott's Revenge option=com_onlinejudge&Itemid=8&page=show_problem&category=59 ...
- uva 11825 Hackers' Crackdown (状压dp,子集枚举)
题目链接:uva 11825 题意: 你是一个黑客,侵入了n台计算机(每台计算机有同样的n种服务),对每台计算机,你能够选择终止一项服务,则他与其相邻的这项服务都终止.你的目标是让很多其它的服务瘫痪( ...
- UVA - 10057 A mid-summer night's dream.
偶数时,中位数之间的数都是能够的(包含中位数) 奇数时,一定是中位数 推导请找初中老师 #include<iostream> #include<cstdio> #include ...
- UVA 10831 - Gerg's Cake(数论)
UVA 10831 - Gerg's Cake 题目链接 题意:说白了就是给定a, p.问有没有存在x^2 % p = a的解 思路:求出勒让德标记.推断假设大于等于0,就是有解,小于0无解 代码: ...
- UVA 12103 - Leonardo's Notebook(数论置换群)
UVA 12103 - Leonardo's Notebook 题目链接 题意:给定一个字母置换B.求是否存在A使得A^2=B 思路:随意一个长为 L 的置换的k次幂,会把自己分裂成gcd(L,k) ...
- UVA 11774 - Doom's Day(规律)
UVA 11774 - Doom's Day 题目链接 题意:给定一个3^n*3^m的矩阵,要求每次按行优先取出,按列优先放回,问几次能回复原状 思路:没想到怎么推理,找规律答案是(n + m) / ...
- uva 10831 - Gerg's Cake(勒让德符号)
题目链接:uva 10831 - Gerg's Cake 题目大意:给定a和p.p为素数,问说是否存在x,使得x2≡a%p 解题思路:勒让德记号,推断ap−12≡1%p #include <cs ...
随机推荐
- 安装github for windows问题解决
到官网下载windows环境下的github,在安装时出现下面问题 An error occurred trying to download 'http://github-windows.s3.ama ...
- poj 3225 间隙(横截面和填充操作)
http://poj.org/problem?id=3225 一道题又做了一天. .这道题对我来说起初有N多难点. 1:区间的开闭怎样解决. . 2:如何把区间的交并补.对称差转化为对线段树的操作. ...
- Hibernate学习笔记(1)Hibernate构造
一 准备工作 首先,我们将创建一个简单的基于控制台(console-based)Hibernate应用. 我们所做的第一件事就是创建我们的开发文件夹.并把所有需要用到的Java件放进去.解压缩从Hib ...
- 纯 Swift 封装的 SQLite 框架:SQLite.swift
SQLite.swift 是一个使用纯 Swift 语言封装 SQLite3 的操作框架. 特性: 简单的查询和参数绑定接口 安全.自动类型数据访问 隐式提交和回滚接口 开发者友好的错误处理和调试 文 ...
- js中推断对象详细类型
大家可能知道js中推断对象类型能够用typeof来推断. 看以下的情况 <script> alert(typeof 1);//number alert(typeof "2&quo ...
- Java拾遗(一):浅析Java子类和父类的实例化顺序 及 陷阱
本文主要介绍Java里经常使用的子类和父类的变量实例化顺序及陷阱,并结合一个Android实例来探讨此问题.日后编程中应尽量避免此陷阱. 首先看以下一段代码: 定义一个虚类Server.java pa ...
- HDU 3032 Nim or not Nim? (需求的游戏SG功能)
意甲冠军:经典Nim游戏转换,给你n礧pi,每个堆栈有pi石头, Alice和Bob轮流石头,意一堆中拿走随意个石子,也能够将某一堆石子分成两个小堆 (每堆石子个数必须不能为0).先拿完者获胜 思路: ...
- 错误21002:[SQL-DMO]用户"xxx"已经存在
原文:错误21002:[SQL-DMO]用户"xxx"已经存在 转载自:http://topic.csdn.net/u/20120829/09/faf381b6-d4e9-4f97 ...
- SQL Server错误代码及解释(留着备用)
原文:SQL Server错误代码及解释(留着备用) 转自:http://www.ajia.me/Article/193.html Code Error Message 0 操作成功完成. 1 功能 ...
- windows cmd命令行下创建文件和文件夹
在window下无法通过 右键=>新建 命令来创建以点开头的文件和文件夹 比如 .log ,会提示必须输入文件名. 可以通过命令行来创建 新建文件夹 mkdir .vs 新建文件 type NU ...