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 4
4) 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 ...
随机推荐
- 认识axure组件区域
组件区域也叫做部件区域,英文为widgets,还有人称之为控件区域,组件是axure事先准备好的网站项目常用的零件,比如一些基本的页面元素 Axure默认存在2个组件库,分别为线框图和流程图.同时我们 ...
- 基于visual Studio2013解决C语言竞赛题之1074八皇后
题目 解决代码及点评 /************************************************************************/ /* ...
- 【环境配置】配置sdk
1. 安装和配置 (1) 下载sdk 官方下载地址http://developer.android.com/sdk/index.html 这里以android-sdk_r12-linux_x86.tg ...
- ASP.NET - 多级分类
表结构: 表数据: 最终效果: 前端代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehin ...
- python输出htmltestrunner中文乱码如何解决
python unittest要产生一个可看的报告,需要借助一个第三方的包 下载HTMLTestRunner.py 第三方库 ,参考地址: http://tungwaiyip.info/softwar ...
- jd.py
#!/usr/bin/env python #coding:utf-8 import urllib2,re,sys,os,types #from bs4 import BeautifulSoup re ...
- 使用commons-daemon启动、关闭java程序
系统环境: CentOS 7 X64 JDK1.8 一: 安装jsvc 下载 commons-daemon的源代码包 http://apache.fayea.com//commons/daemon/s ...
- OSGi 学习之路(4) - osgi的模块化 java在模块化的局限性
底层代码可见性控制 Java提供了private,public,protected和package private(无修饰符)这四种访问控制级别,不过这仅仅提供了底层的OO数据封装特性.包这个概念确实 ...
- 让工程师爱上CMMI,实现管理于无形 --- 中标软件CMMI L5之路 (1/2)
操作系统市场被微软等国外的IT厂商垄断的大环境下,中标软件作为市场夹缝中发展起来的民族企业,致力于成为中国操作系统旗舰企业.系列核心产品已经在政府.金融.教育.财税.公安.审计.交通.医疗.制造等行业 ...
- Ajax - 异步处理(点击变成文本框并修改)
效果: 对应的文档结构: Test.aspx 前台代码: 引入JQuery(jquery-1.8.3.min.js). 引入自己所写的JS代码(UserJS.js). <html xmlns=& ...