Don't Get Rooked
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 2086   Accepted: 1325

Description

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 4x4) 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 is legal 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 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 integer n that is the size of the board; n will be at most 4. The next n 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.

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
题目大意:在一个有着可以阻碍攻击的墙的棋盘上摆放车,使其相互之间不能攻击,输出能够摆放车的最大数量。
解题方法:用DFS直接暴搜。
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std; int ans;
char maze[][];
int visited[][];
int n; void DFS(int sum)
{
ans = max(ans, sum);
for (int i = ; i < n; i++)
{
for (int j = ; j < n; j++)
{
bool bflag = true;
for (int k = i; k < n; k++)
{
if (visited[k][j])
{
bflag = false;
break;
}
else
{
if (maze[k][j] == 'X')
{
break;
}
}
}
for (int k = i - ; k >= ; k--)
{
if (visited[k][j])
{
bflag = false;
break;
}
else
{
if (maze[k][j] == 'X')
{
break;
}
}
}
for (int k = j; k < n; k++)
{
if (visited[i][k])
{
bflag = false;
break;
}
else
{
if (maze[i][k] == 'X')
{
break;
}
}
}
for (int k = j - ; k >= ; k--)
{
if (visited[i][k])
{
bflag = false;
break;
}
else
{
if (maze[i][k] == 'X')
{
break;
}
}
}
if (bflag && maze[i][j] != 'X')
{
visited[i][j] = ;
DFS(sum + );
visited[i][j] = ;
}
}
}
} int main()
{
while(cin>>n)
{
if (n == )
{
break;
}
ans = ;
for (int i = ; i < n; i++)
{
cin>>maze[i];
}
memset(visited, , sizeof(visited));
DFS();
printf("%d\n", ans);
}
return ;
}

最新文章

  1. sql case when...then...else...end 选择判断
  2. ES6 学习笔记(1)
  3. loadrunner协议选择
  4. 解决在android开发中ViewPager中Gallery无法滑动问题
  5. Windows下记事本编辑的Shell脚本放到Linux下执行出错,格式问题(/bin/bash^M: bad interpreter: 没有那个文件或目录)
  6. 注册表中LEGACY残留项的清理技巧
  7. 理解php的opcode
  8. C# 基础之类型(一)
  9. Xamarin android SwipeRefreshLayout入门实例
  10. 如何在以太坊上搭建一个Dapp?
  11. shell脚本学习-变量
  12. 【AtCoder】ARC083
  13. JAVA4种线程池的使用
  14. LeetCode 929 Unique Email Addresses 解题报告
  15. 1-自己动手编写ArrayList集合
  16. 在mvc中动态加载菜单
  17. Java Eclipse编译后产生的字节码文件,用DOS命令符怎么打开
  18. Launch Google Map in Android / IOS Mobile
  19. 启用lazyload插件,减少图片加载
  20. Linux kill/pkill/killall命令详解

热门文章

  1. SQL Server数据库所有表重建索引
  2. JavaScript_5_对象
  3. ABAP function group和Tomcat library重复加载问题
  4. 【UML】用例图Use Case diagram(转)
  5. CF Gym 100187B A Lot of Joy (古典概型)
  6. js中读取解析json数据
  7. Java 从资源文件(.properties)中读取数据
  8. Servlet的引入(即加入Servlet)
  9. 如何解决U盘装系统后磁盘总容量变小?
  10. XAMPP vhost 配置(403问题解决)