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