Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 4034   Accepted: 1352

Description

Consider the following 5 picture frames placed on an 9 x 8 array.

........ ........ ........ ........ .CCC....

EEEEEE.. ........ ........ ..BBBB.. .C.C....

E....E.. DDDDDD.. ........ ..B..B.. .C.C....

E....E.. D....D.. ........ ..B..B.. .CCC....

E....E.. D....D.. ....AAAA ..B..B.. ........

E....E.. D....D.. ....A..A ..BBBB.. ........

E....E.. DDDDDD.. ....A..A ........ ........

E....E.. ........ ....AAAA ........ ........

EEEEEE.. ........ ........ ........ ........

1 2 3 4 5

Now place them on top of one another starting with 1 at the bottom and ending up with 5 on top. If any part of a frame covers another it hides that part of the frame below.

Viewing the stack of 5 frames we see the following.

.CCC....

ECBCBB..

DCBCDB..

DCCC.B..

D.B.ABAA

D.BBBB.A

DDDDAD.A

E...AAAA

EEEEEE..

In what order are the frames stacked from bottom to top? The answer is EDABC.

Your problem is to determine the order in which the frames are stacked from bottom to top given a picture of the stacked frames. Here are the rules:

1. The width of the frame is always exactly 1 character and the sides are never shorter than 3 characters.

2. It is possible to see at least one part of each of the four sides of a frame. A corner shows two sides.

3. The frames will be lettered with capital letters, and no two frames will be assigned the same letter.

Input

Each input block contains the height, h (h<=30) on the first line and the width w (w<=30) on the second. A picture of the stacked frames is then given as h strings with w characters each. 
Your input may contain multiple blocks of the format described above, without any blank lines in between. All blocks in the input must be processed sequentially.

Output

Write the solution to the standard output. Give the letters of the frames in the order they were stacked from bottom to top. If there are multiple possibilities for an ordering, list all such possibilities in alphabetical order, each one on a separate line. There will always be at least one legal ordering for each input block. List the output for all blocks in the input sequentially, without any blank lines (not even between blocks).

Sample Input

9
8
.CCC....
ECBCBB..
DCBCDB..
DCCC.B..
D.B.ABAA
D.BBBB.A
DDDDAD.A
E...AAAA
EEEEEE..

Sample Output

EDABC

Source

[Submit]   [Go Back]   [Status]   [Discuss]

解题思路:由于每条边都能看见部分,那么我们就可以根据所给图得出每张图片的具体位置,然后根据拓扑+DFS得出所有结果,最后再按字典序排下序即可输出答案。

解题代码:

 #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;
const int max_n = ;
struct data{
int max_x, min_x;
int max_y, min_y;
}data[max_n]; struct Node{
int cover;
Node *next;
};
Node *Link[max_n], *tm_node;
char map[][], ch;
bool vis[max_n];
int cnt[max_n];
string ans[];
int pos;
int n, m, num; int Max(int a, int b){
return a > b ? a : b;
} int Min(int a, int b){
return a > b ? b : a;
} void find(){
int i, j, k;
for (i = ; i < max_n; i ++){
if(vis[i]){
for(j = data[i].min_x; j <= data[i].max_x; j ++){
if(j == data[i].min_x || j == data[i].max_x){
for(k = data[i].min_y; k <= data[i].max_y; k ++){
ch = map[j][k] - 'A';
if(ch != i){
tm_node = new Node;
tm_node ->cover = i;
tm_node ->next = NULL;
cnt[i] ++;
if(Link[ch] == NULL)
Link[ch] = tm_node;
else{
tm_node ->next = Link[ch];
Link[ch] = tm_node;
}
}
}
}
else{
k = data[i].min_y;
ch = map[j][k] - 'A';
if(ch != i){
tm_node = new Node;
tm_node ->cover = i;
tm_node ->next = NULL;
cnt[i] ++;
if(Link[ch] == NULL)
Link[ch] = tm_node;
else{
tm_node ->next = Link[ch];
Link[ch] = tm_node;
}
}
k = data[i].max_y;
ch = map[j][k] - 'A';
if(ch != i){
tm_node = new Node;
tm_node ->cover = i;
tm_node ->next = NULL;
cnt[i] ++;
if(Link[ch] == NULL)
Link[ch] = tm_node;
else{
tm_node ->next = Link[ch];
Link[ch] = tm_node;
}
}
}
}
}
}
} void DFS(int deep, string tm_ans){
if(deep == num){
ans[pos] = "";
for (int j = tm_ans.length() -; j >= ; j --)
ans[pos] += tm_ans[j];
pos ++;
return;
}
for(int i = ; i < max_n; i ++){
if(vis[i] && cnt[i] == ){
for(tm_node = Link[i]; tm_node != NULL; tm_node = tm_node ->next)
cnt[tm_node ->cover] --;
cnt[i] = -;
ch = i + 'A';
DFS(deep +, tm_ans + ch);
cnt[i] = ;
for(tm_node = Link[i]; tm_node != NULL; tm_node = tm_node ->next)
cnt[tm_node ->cover] ++;
}
}
} int main(){
int i, j;
while(~scanf("%d%d", &n, &m)){
num = ;
memset(vis, , sizeof(vis));
memset(data, -, sizeof(data));
memset(cnt, , sizeof(cnt));
memset(Link, , sizeof(Link));
for(i = ; i < n; i ++)
scanf("%s", map[i]);
for(i = ; i < n; i ++){
for(j = ; j < m; j ++){
char ch = map[i][j];
if(ch == '.')
continue;
ch -= 'A';
if(vis[ch] == )
num ++;
vis[ch] = ;
int tm = data[ch].max_x;
data[ch].max_x = (tm == - ? i : Max(tm, i));
tm = data[ch].min_x;
data[ch].min_x = (tm == - ? i : Min(tm, i));
tm = data[ch].max_y;
data[ch].max_y = (tm == - ? j : Max(tm, j));
tm = data[ch].min_y;
data[ch].min_y = (tm == - ? j : Min(tm, j));
}
}
pos = ;
find();
DFS(, "");
sort(ans, ans +pos);
for (i = ; i < pos; i ++)
cout << ans[i] << endl;
}
return ;
}

Frame Stacking ZOJ 1083,poj 1128的更多相关文章

  1. zoj 2358,poj 1775 Sum of Factorials(数学题)

    题目poj 题目zoj //我感觉是题目表述不确切,比如他没规定xi能不能重复,比如都用1,那么除了0,都是YES了 //算了,这种题目,百度来的过程,多看看记住就好 //题目意思:判断一个非负整数n ...

  2. POJ 1128 Frame Stacking(拓扑排序&#183;打印字典序)

    题意  给你一些矩形框堆叠后的鸟瞰图  推断这些矩形框的堆叠顺序  每一个矩形框满足每边都至少有一个点可见  输入保证至少有一个解 按字典序输出全部可行解 和上一题有点像  仅仅是这个要打印全部的可行 ...

  3. HDU - 1083 Courses /POJ - 1469

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1083 http://poj.org/problem?id=1469 题意:给你P个课程,并且给出每个课 ...

  4. Frame Stacking(拓扑排序)

    题目链接:http://acm.tju.edu.cn/toj/showp1076.html1076.   Frame Stacking Time Limit: 1.0 Seconds   Memory ...

  5. HDU 1815, POJ 2749 Building roads(2-sat)

    HDU 1815, POJ 2749 Building roads pid=1815" target="_blank" style="">题目链 ...

  6. HDU 1325,POJ 1308 Is It A Tree

    HDU认为1>2,3>2不是树,POJ认为是,而Virtual Judge上引用的是POJ数据这就是唯一的区别....(因为这个瞎折腾了半天) 此题因为是为了熟悉并查集而刷,其实想了下其实 ...

  7. HDU 1816, POJ 2723 Get Luffy Out(2-sat)

    HDU 1816, POJ 2723 Get Luffy Out pid=1816" target="_blank" style="">题目链接 ...

  8. POJ 1128 Frame Stacking (拓扑排序)

    题目链接 Description Consider the following 5 picture frames placed on an 9 x 8 array. ........ ........ ...

  9. zoj 1083 Frame Stacking

    其实就是一个拓补排序.(动态记录第i个之上的j存不存在,反过来就是第j个之下的i) 首先确立每个框的位置(题目明确说了每一边都不会被完全覆盖)./*可以通过搜索,搜索到该框的所有四个角*/||如果题目 ...

随机推荐

  1. Oracle字符乱码、数据越界訪问典型Bug分析

    Oracle字符乱码.数据越界訪问典型Bug分析 前言:           作为乙方,在甲方客户那里验收阶段发现两个诡异Bug. 下面就问题来源.问题根因.解决方式.怎样避免做具体描写叙述. .且两 ...

  2. Linux以下的两种文件锁

    文件锁是一种文件读写机制.在不论什么特定的时间仅仅同意一个进程訪问一个文件. 利用这样的机制可以使读写单个文件的过程变得更安全. 在这篇文章中.我们将探讨Linux中不同类型的文件锁,并通过演示样例程 ...

  3. dpdk l2fwd 应用流程分析

    int MAIN(int argc, char **argv) { struct lcore_queue_conf *qconf; struct rte_eth_dev_info dev_info; ...

  4. 高级程序员与CTO技术总监首席架构师

    一.高级程序员 如果你是一个刚刚创业的公司,公司没有专职产品经理和项目经理,你就是公司的产品经理,你如果对你现在的开发员能力不满,那么你只需要的是一个高级程序员. 你定义功能.你做计划推进和管理,他可 ...

  5. mount ntfs 失败解决办法

    在双系统中,ntfs可能会应为windows的缓存而挂载失败.可用下面命令修复. Use ntfsfix in the terminal, even if you can't access Windo ...

  6. JavaScript中Math常用方法

    title: JavaScript中Math常用方法 toc: false date: 2018-10-13 12:19:31 Math.E --2.718281828459045,算数常量e Mat ...

  7. node,koa 图片批量添加水印,可手动配置水印位置

    公司设计在处理京东上架商品图片的时候,需要给设计好的图片添加京东的“logo”,并且logo位置得根据图片来摆放,需要通过计算得出logo位置.那样太麻烦了,于是就用node,koa写了批量给图片添加 ...

  8. PHP简介 变量 输出

    一.PHP概念 Hypertext Preprocessor 超文本预处理器,是一种开源脚本语言,语法吸收了C语言,Java,Perl的特点,用于web开发领域, PHP是将程序嵌入到Html文档中执 ...

  9. CSS3-----transform 转换

    transforn  可以转换元素,其中主要属性有:rotate() / skew() / scale() / translate()以下4种. transform:rotate():旋转:其中“de ...

  10. 使用3ds Max制作卡通狗教程

    使用软件::3ds Max 软件下载:http://www.xy3dsmax.com/xiazai.html 全教程完,学完记得交作业.如果本教程对您有所帮助,请推荐给你的朋友. 全教程完,学完记得交 ...