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 ...
随机推荐
- 【虚拟化实战】容灾设计之一VR vs SRM
作者:范军 (Frank Fan) 新浪微博:@frankfan7 从本文开始,我们将介绍一系列的关于容灾的解决方案.先探讨应用的场景,然后再深入介绍技术架构. 情景一: 某小型公司的虚拟化环境中,在 ...
- Reader开发(二)增加PDF阅读功能
最近任务很多很忙,所以更新博客的速度很慢. 大概上周就为Reader加了一个PDF阅读的功能,但是一直没时间写上来.昨晚找一下文件发现扩展了功能的Demo居然在文件目录下看不到任何文件,但是却显示有文 ...
- 初入Android--环境搭建
Android SDK 可以下载adt-bundle:包含了装好插件的eclipse和android sdk.下载好后,首先设置ANDROID_HOME环境变量:ANDROID_HOME=/home/ ...
- 利用VS2005进行dump文件调试(17篇博客)
前言:利用drwtsn32或NTSD进行程序崩溃处理,都可以生成可用于调试的dmp格式文件.使用VS2005打开生成的DMP文件,能很方便的找出BUG所在位置.本文将讨论以下内容: 1. 程序编译选 ...
- 全方位深度剖析--性能测试之LoardRunner 介绍
一.介绍 LoardRunner是一种预测系统行为和性能负载的测试工具.通过模拟上千万用户实施并发负载及实时性能监控的方式来确认和查找系统的瓶颈,LoardRunner能够对整个企业架构进行测试.通过 ...
- HDU 3397 Sequence operation(线段树)
HDU 3397 Sequence operation 题目链接 题意:给定一个01序列,有5种操作 0 a b [a.b]区间置为0 1 a b [a,b]区间置为1 2 a b [a,b]区间0变 ...
- ANTLR4权威參考手冊(一)
写在前面的话: 此文档是对伟大的Terence Parr的著作<the definitive antlr4 reference>的翻译本.致敬!欢迎转载,请注明原地址,请尊重劳动成果.翻译 ...
- 用angularjs开发下一代web应用(二):angularjs应用骨架(二)
1.浅谈非入侵式JavaScript <div ng-click="doSomething()">...</div>这些指令和原来的事件处理器有下面不同之处 ...
- bonjour
首先bonjour并非必须的,可是它的确非常方便,假设没有它我们须要指定ip地址进行局域网的传输,有了它就能够依据服务的详细的名称来选择服务,能够这样来理解bonjour就相当于hostname,我们 ...
- cPickle.so:: PyUnicodeUCS2_DecodeUTF8
cPickle.so:: PyUnicodeUCS2_DecodeUTF8错误 Python编译的参数,和Python module(mod_wsgi, pymodwsgi)编译参数不一,导致一些un ...