UVA 639 (13.08.25)
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
题意:
类似八皇后的一道题, 但是这道题多了一个"墙"的概念, 使得一行放置多辆车变成可能的(对列也是)
而且, 同一条斜线是允许多辆车的~
这里写下我解题的心得, 算是记下笔记了:
这是一道回溯题, 刘汝佳算法书上对回溯的描述是"在递归构造中, 生成和检查过程可以有机的结合起来, 从而减少不必要的枚举, 这就是回溯法";
回溯法何时应用?
只要能把待求解的问题分成不太多的步骤, 每个步骤又只有不太多的选择, 都应该考虑使用回溯法~
做法:
每个点都有两种选择, 停车或者不停车, 所以可以根据这个递归
然后, 中途是要判断是否可以停车的, 不是每个点想停就能停, 所以我写了一个 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;
}
UVA 639 (13.08.25)的更多相关文章
- UVA 10340 (13.08.25)
Problem E All in All Input: standard input Output: standard output Time Limit: 2 seconds Memory Limi ...
- UVA 10041 (13.08.25)
Problem C: Vito's family Background The world-known gangster Vito Deadstone is moving to New York. ...
- UVA 10194 (13.08.05)
:W Problem A: Football (aka Soccer) The Problem Football the most popular sport in the world (ameri ...
- UVA 10499 (13.08.06)
Problem H The Land of Justice Input: standard input Output: standard output Time Limit: 4 seconds In ...
- UVA 253 (13.08.06)
Cube painting We have a machine for painting cubes. It is supplied withthree different colors: blu ...
- UVA 156 (13.08.04)
Ananagrams Most crossword puzzle fans are used to anagrams--groupsof words with the same letters i ...
- UVA 573 (13.08.06)
The Snail A snail is at the bottom of a 6-foot well and wants to climb to the top.The snail can cl ...
- UVA 10025 (13.08.06)
The ? 1 ? 2 ? ... ? n = k problem Theproblem Given the following formula, one can set operators '+ ...
- UVA 465 (13.08.02)
Overflow Write a program that reads an expression consisting of twonon-negative integer and an ope ...
随机推荐
- Codeforces Round #254 (Div. 2)D(预计)
D. DZY Loves FFT time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- C#压缩与解压
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- 不包含SDK头文件, 补全API定义
/// @file main.cpp /// @brief 不包含SDK头文件, 补全API定义 #ifdef __cplusplus extern "C" { #endif /* ...
- pygame编写贪吃蛇
一直想用pygame做一个小游戏的,可是因为拖延症的缘故一直没有动,结果那天看到了一个12岁的国际友人小盆友用pygame做的一款塔防游戏,突然感觉已经落后超级远了,所以心血来潮做小游戏了.高中陪伴我 ...
- log4j的性能瓶颈定位与性能优化(org.apache.log4j.spi.RootLogger) (转)
最近执行一个项目调优,发现使用第三方的Json库导致性能差.原以为问题就这么定位到了,结果去掉Json操作后,性能也不见好转. 现象非常诡异:CPU.内存.网络.磁盘使用率均有剩余,而且压力也是足够的 ...
- zabbix 获取jvm session信息
zabbix:/root# java -jar /root/cmdline-jmxclient-0.10.3.jar - 121x:5566 "Catalina:type=Manager,c ...
- Cocos2dx中Plugin-X 在android下的整合
直接拉plugin-x中的jar包导入到Eclipse中就可以.用这么麻烦的工具干嘛.
- React-TodoList
React入门最好的学习实例-TodoList 前言 React 的核心思想是:封装组件,各个组件维护自己的状态和 UI,当状态变更,自动重新渲染整个组件. 最近前端界闹的沸沸扬扬的技术当属react ...
- T-SQL 操作文件 具体解释
/******* 导出到excel EXEC master..xp_cmdshell 'bcp SettleDB.dbo.shanghu out c:\temp1.xls -c -q -S" ...
- hibernate.cfg.xml文件的配置模板和不同数据库的配置參数
(1)hibernate.cfg.xml文件的配置模板 <?xml version="1.0" encoding="UTF-8"?> <!DO ...