UVa 639 Don't Get Rooked:DFS好题

639 - Don't Get Rooked

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_problem&problem=580

In chess, the rook is a piece that can move any number of squares vertically or horizontally. In this problem we will consider small chess boards (at most 4

4) that can also contain walls through which rooks cannot move. The goal is to place as many rooks on a board as possible so that no two can capture each other. A configuration of rooks islegal provided that no two rooks are on the same horizontal row or vertical column unless there is at least one wall separating them.

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 rooks 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 board, calculates the maximum number of rooks that can be placed on the board in a legal configuration.

Input

The input file contains one or more board descriptions, followed by a line containing the number 0 that signals the end of the file. Each board description begins with a line containing a positive integern that is the size of the board; n will be at most 4. The nextn lines each describe one row of the board, 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 rooks that can be placed on the board 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

使用-1表示墙,0表示没有车,1表示有车,使用dfs,注意换行遍历。

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索number
, configuration
, board
, of
, The
that
don t you get it、don t get me wrong、don t get pervy、don t you get it 岚、don t you get it歌词,以便于您获取更多的相关知识。

时间: 2024-12-30 18:45:58

UVa 639 Don't Get Rooked:DFS好题的相关文章

UVa 639:Don't Get Rooked, 类八皇后问题

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_problem&problem=580 题目类型: 暴力, 回溯法 题目: In chess, the rook is a piece that can move any number of squares vertically or horizontally. In this p

UVa 839 Not so Mobile:DFS二叉树

839 - Not so Mobile Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_problem&problem=780 Before being an ubiquous communications gadget, a mobile was just a structure mad

UVa 699 The Falling Leaves:DFS遍历二叉树

699 - The Falling Leaves Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=640 Each year, fall in the North Central region is accompanied by the brilliant colors of the lea

UVa 10616 Divisible Group Sums:DFS以及DP

10616 - Divisible Group Sums Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1557 思路:用DFS+记忆化搜索枚举组合,注意数字有<0的. return dp[i][cnt][sum] = f(i + 1, cnt +

uva 10422 - Knights in FEN ID+dfs

10422 - Knights in FEN        经典的棋盘类问题,移动骑士到达指定状态,最少要几次,如果超过10次则输出Unsolvable in less than 11 move(s).        一开始想bfs的,但是状态量比较大,又懒得写hash,又想到了最坏情况,也就是超出10次时,dfs和bfs复杂度都一样,于是就果断用了ID+dfs,编程难度非常低.      不用加剪枝就可以过了,我的代码里加了个最优下限,这样不用每次从0开始进行ID,刚有想到可以利用当前最优情况

UVa 11205 The broken pedometer:枚举好题&amp;amp;巧用二进制

11205 - The broken pedometer Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=107&page=show_problem&problem=2146 The Problem A marathon runner uses a pedometer with which he is having

UVa 10400

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1341 类型:回溯 原题: A game show in Britain has a segment where it gives its contestants a sequence of positive numbers and a target

UVa 10274:Fans and Gems, 神牛Rujia Liu的神题(三)

题目链接: UVa :  http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1215 zoj    : http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1237 类型:  隐式图搜索+恶心模拟+哈希判重+bfs+dfs 原题: Tomy's

UVa 10176 Ocean Deep !

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1117 思路和UVa 11344是一样的,只不过那题是10进制,这题是2进制. 完整代码: 01./*0.019s*/ 02. 03.#include<cstdio> 04. 05.int main() 06.{ 07. int mod; 08. c