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.
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.
For each test case, output one line containing themaximum number of rooks that can be placed on the boardin a legal configuration.
4
.X..
....
XX..
....
2
XX
.X
3
.X.
X.X
.X.
3
...
.XX
.XX
4
....
....
....
....
0
5
1
5
2
4
题意:
类似八皇后的一道题, 但是这道题多了一个"墙"的概念, 使得一行放置多辆车变成可能的(对列也是)
而且, 同一条斜线是允许多辆车的~
这里写下我解题的心得, 算是记下笔记了:
这是一道回溯题, 刘汝佳算法书上对回溯的描述是"在递归构造中, 生成和检查过程可以有机的结合起来, 从而减少不必要的枚举, 这就是回溯法";
回溯法何时应用?
只要能把待求解的问题分成不太多的步骤, 每个步骤又只有不太多的选择, 都应该考虑使用回溯法~
做法:
每个点都有两种选择, 停车或者不停车, 所以可以根据这个递归
然后, 中途是要判断是否可以停车的, 不是每个点想停就能停, 所以我写了一个 isOK 的判断函数
AC代码:
#include<stdio.h>
int max;
int vis[10][10];
char str[10];
int isOK(int x, int y) {
int l, u;
for(l = y-1; l >= 0; l--) {
if(vis[x][l] == -1)
break;
if(vis[x][l] == 0)
return 0;
}
for(u = x-1; u >= 0; u--) {
if(vis[u][y] == -1)
break;
if(vis[u][y] == 0)
return 0;
}
return 1;
}
void DFS(int x, int y, int nline, int count) {
while(x < nline) {
if(vis[x][y] == 1 && isOK(x, y)) {
vis[x][y] = 0;
count++;
DFS(x, y+1, nline, count);
vis[x][y] = 1;
count--;
}
if(y >= nline - 1) {
y = 0;
x++;
}
else
y++;
}
if(count >= max)
max = count;
}
int main() {
int n;
while(scanf("%d", &n) != EOF && n) {
max = 0;
for(int i = 0; i < n; i++) {
scanf("%s", str);
for(int j = 0; j < n; j++) {
if(str[j] == '.')
vis[i][j] = 1;
else
vis[i][j] = -1;
}
}
DFS(0, 0, n, 0);
printf("%d\n", max);
}
return 0;
}
分享到:
相关推荐
uva272
UVa在我看来是比较全的一个题解,希望能帮助大家。欢迎下载。
UVA109的题解,经测试完全正确,还附有题解。
有uva刘汝佳文件夹的50道题解,从数据结构开始,以后慢慢上传
包含UVA在线OJ系统的绝大部分的示例代码,并都已AC,可在刷题时参考
uva最全ac代码
1.Uva_base的编译 在编译球队时,则需要在当前球队文件夹下打开终端输入执行以下命令(以下命令都是在root下执行的): ./configure make clean make 如果运行Uva_base后,出现球员越界或掉线的情况,就重新...
uva531最长公共子序列问题水题,应用简单的dp即可ac有更快速的方法欢迎讨论
这是一支完整的uva球队,包含所有基本模块,初者可在上修改得到自己的球队
UVA 题目,不是很难,试试吧
《算法竞赛入门经典》UVa配套题目pdf版完整
uva357的栈实现版本
uva10755 ac 代码,可以随意更改下载
世界著名大学UVA OJ平台上的题目部分分类,分的不好请原谅。
开源项目-codingsince1985-UVa.zip,Been solving UVa Online Judge Problems in Golang for one year (and counting)
这里面全部为在Uva Online Judge上面的部分题目的解答,里面提供了解答使用的源代码。
uva_trilearn2002 源代码
主要是uvaoj习题相关题目 练习题目
UVA 499 Solution in C/ C++
算法竞赛入门经典(第二版)的习题都是UVa上的, 但是UVa太慢了太慢了太慢了太慢了太慢了, 于是我把各章习题的pdf一次性打包下载到本地, 和大家分享:)