UVA11846-Finding Seats Again(DFS)
Accept: 69 Submit: 433
Time Limit: 10000 mSec
Problem Description
A set of n2 computer scientists went to the movies. Fortunately, the theater they chose has a square layout: n rows, each one with n seats. However, these scientists are not all from the same research area and they want to seat together. Indeed, there are K independent research groups of scientists among them (no scientist belongs to two of them) with a distiguished leader for each group. Then the leader bought the tickets for his whole group, and he did it in such a way that all his group could seat occupying a rectangular set of seats (and everyone in this set of seats belongs to the same group). Every group was placed satisfying this bizarre condition, although the scientists did not care where the actual assigned areas were. The usher was informed of the situation and he decided to annotate in a theater map a satisfactory seats deploying. He thought that if he wrote the position of each group’s leader in the map indicating besides the corresponding group size, he could tell where to accomodate every scientist. But he discovered that it is not so easy! The usher asks for your help. You must tell him a way to place the K rectangular areas with the given sizes, and with the corresponding leader for each group seated where it was originally assigned.
Input
Input consists of several test cases, each one defined by a set of lines:
• the first line in the case contains two numbers n and K separated by blanks, with n representing the size of the theater (0 < n < 20) and K the number of groups (K ≤ 26);
• the next n lines describe the usher’s map. A one-digit decimal number in the map indicates the seat of a leader and the size of his group. A point indicates that no leader will sit there.
The end of the input is indicated by the line
0 0
Output
For each test case, display an answer consisting in n lines each one of them with n characters representing a seat occupation for the theater. Each group is assigned to an uppercase letter and all of its members are identified with that letter. No two groups are assigned to the same letter.
Sample Input
3.4
...
.2.
7 18
...4.2.
...45..
222..3.
...2..3
.24...2
...2.3.
22..3..
0 0
Sample Output
ABB
ABB
ACC
AAAABCC
DDDDBEF
GHIIBEF
GHJKBEF
LLJKBMM
NOJPQQQ
NOJPRRR
题解:这个题还是挺有价值的,看到这个题目,第一时间想到了UVA211的那个多米诺效应那个题,但是这两个题除了题意有点相似之外感觉就没啥相同的了(虽然都是DFS),一开始的思路围绕是数字的格子展开,这个思路在填字母的时候就有很大的困难,这个是数字的格子位于这个矩形的哪里,这个矩形的长宽分别是多少,这两个问题使得这个思路几乎就行不通了。最后参考了大佬的题解(orz),发现他不是从是数字的格子开始扩展,而是直接顺着从没有填过字母的格子开始扩展,扩展的范围很清楚,就是先枚举行,再枚举列,对于枚举中一个给定的矩形,首先这里不能有字母,其次有且仅有一个数字,并且数字的大小等于矩形的面积,满足了这些,就是一个可以继续深层递归的状态,接着dfs下去。这里在枚举的过程中有一个不错的减少枚举的方法,就是如果先枚举行,那么列的最大值随着行的增加一定是不增的(原因很简单,详见代码),这样就可以随时改变列的最大值从而减少枚举。
#include <bits/stdc++.h> using namespace std; const int maxn = ;
const int INF = 0x3f3f3f3f; int n, k;
char gra[maxn][maxn], ans[maxn][maxn]; bool dfs(int id, char ch) {
while (ans[id / n][id % n] != '.') id++;
if (id == n * n) return true; int sr = id / n, sc = id % n, ec = n;
for (int r = sr; r < n; r++) {
for (int c = sc; c < ec; c++) {
if (ans[r][c] != '.') { ec = c; break; }
int sum = (r - sr + )*(c - sc + );
int num = INF;
bool ok = true;
for (int i = sr; i <= r; i++) {
for (int j = sc; j <= c; j++) {
if (isdigit(gra[i][j])) {
if (num != INF) { ok = false; break; }
else num = gra[i][j] - '';
}
}
if (!ok) break;
}
if (!ok || sum > num) { ec = c; break; }
if (sum < num) continue; for (int i = sr; i <= r; i++) {
for (int j = sc; j <= c; j++) {
ans[i][j] = ch;
}
}
if (dfs(id + c - sc + , ch + )) return true;
for (int i = sr; i <= r; i++) {
for (int j = sc; j <= c; j++) {
ans[i][j] = '.';
}
}
}
}
return false;
} int main()
{
//freopen("input.txt", "r", stdin);
while (~scanf("%d%d", &n, &k) && (n || k)) {
for (int i = ; i < maxn; i++) {
for (int j = ; j < maxn; j++) {
ans[i][j] = '.';
}
}
for (int i = ; i < n; i++) {
scanf("%s", gra[i]);
} dfs(, 'A'); for (int i = ; i < n; i++) {
for (int j = ; j < n; j++) {
printf("%c", ans[i][j]);
}
printf("\n");
}
}
return ;
}
UVA11846-Finding Seats Again(DFS)的更多相关文章
- hdu1937 Finding Seats
hdu1937 Finding Seats 题意是 求最小的矩形覆盖面积内包含 k 个 空位置 枚举上下边界然后 双端队列 求 最小面积 #include <iostream> #incl ...
- HDU 1937 F - Finding Seats 枚举
F - Finding Seats Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- UVa 11846 - Finding Seats Again
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- hdu 1937 Finding Seats
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- HDU 4414 Finding crosses(dfs)
Problem Description The Nazca Lines are a series of ancient geoglyphs located in the Nazca Desert in ...
- 杭电ACM分类
杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...
- Soj题目分类
-----------------------------最优化问题------------------------------------- ----------------------常规动态规划 ...
- 转载:hdu 题目分类 (侵删)
转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...
- poj 3740 Easy Finding 二进制压缩枚举dfs 与 DLX模板详细解析
题目链接:http://poj.org/problem?id=3740 题意: 是否从0,1矩阵中选出若干行,使得新的矩阵每一列有且仅有一个1? 原矩阵N*M $ 1<= N <= 16 ...
随机推荐
- JVM运行时数据区内容简述
JVM运行时数据区分为五个部分:程序计数器.虚拟机栈.本地方法栈.堆.方法区.如下图所示,五部分其中又分为线程共享区域和线程私有区域,下面将分别介绍每一部分. 1. PC程序计数器 程序计数器是一块较 ...
- Java岗 面试考点精讲(基础篇01期)
即将到来金三银四人才招聘的高峰期,渴望跳槽的朋友肯定跟我一样四处找以往的面试题,但又感觉找的又不完整,在这里我将把我所见到的题目做一总结,并尽力将答案术语化.标准化.预祝大家面试顺利. 术语会让你的面 ...
- angular raido checkbox select取值
radio {{modelName}} <div class="radio disIB"> <label class="i-checks"&g ...
- canvas-a11htmlANDcanvas.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- JS怎样实现图片的懒加载以及jquery.lazyload.js的使用
在项目中有时候会用到图片的延迟加载,那么延迟加载的好处是啥呢? 我觉得主要包括两点吧,第一是在包含很多大图片长页面中延迟加载图片可以加快页面加载速度:第二是帮助降低服务器负担. 下面介绍一下常用的延迟 ...
- 95%的中国网站需要重写CSS
95%的中国网站需要重写CSS 很长一段时间,我都使用12px作为网站的主要字体大小.10px太小,眼睛很容易疲劳,14px虽容易看清,却破坏页面的美感.唯独12px在审美和视力方面都恰到好处. 谁对 ...
- BGP 优选短的AS号路径实践总结
BGP优选AS号短的路径作为首选路由的验证结果 1.结论: (1)EBGP会优选AS号少的路径作为转发路径,当优选路径失效时,再选择次选路由. (2)EBGP邻居建立在直连设备之间. (3)IBGP邻 ...
- 不能用notepad++编辑器编写python
不能用notepad++编辑器编写python,因为notepad对空格支持不是很良好,会出现莫名其妙的错误!建议用vim或emacs. 有人这样解释:不要混合使用制表符和空格来缩进,因为这在跨越不同 ...
- java8中Lambda表达式和Stream API
一.Lambda表达式 1.语法格式 Lambda是匿名函数,可以传递代码.使用“->”操作符,改操作符将lambda分成两部分: 左侧:指定了 Lambda 表达式需要的所有参数 右侧:指定了 ...
- C#-基本语法(三)
关键词 关键字,是一些被C#规定了用途的重要单词 在Visual Studio的开发环境中,关键字被标识为蓝色 例如: using:导入命名空间 class:声明类 static:静态 void:无返 ...