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. Qt之窗体透明

    简述 关于窗体透明,经常遇到,下面我们针对常用的透明效果进行讲解: 全透明(主窗体.子窗体均透明) 主窗体透明(子窗体不透明) 子窗体透明(主窗体不透明) 简述 正常状态 全透明 效果 源码 主窗体透 ...

  2. iOS开发中的NSDateFormatter日期格式解析总结

    在工作中,常常遇到将时间解析出来转换成自己相应要求的时间格式,之前也有收集相应的转换格式,如今将自己收集的一部分了做个分享,应该比較完好了,欢迎大家继续补充 年 y 将年份 (0-9) 显示为不带前导 ...

  3. 浅析JAVA设计模式之工厂模式(二)

    1 工厂方法模式简单介绍 工厂方法 (Factroy Method)模式:又称多态性工厂模式(Polymorphic Factory),在这样的模式中,核心工厂不再是一个详细的类.而是一个抽象工厂,提 ...

  4. 服务器共享session的方式

    服务器共享session的方式 简介 1. 基于NFS的Session共享 NFS是Net FileSystem的简称,最早由Sun公司为解决Unix网络主机间的目录共享而研发.这个方案实现最为简单, ...

  5. FriendlyARM交叉工具链以及编译第一个arm9应用

    不记录什么都会忘光!!!这两天又要用到开发板来做项目,可是好久没有碰了,最近一直在搞上层的东东,对rails和前端感兴趣,我这是不要毕业的节奏了吗?好吧,既然什么都忘光掉了,那就干脆来个痛快,重新装机 ...

  6. navigator.clipboard 浏览器原生剪贴板

    浏览器原生剪贴板 navigator.clipboard 写入 navigator.clipboard.writeText navigator.clipboard.writeText('Linr Te ...

  7. handsontable在线编辑excel扩展功能-踩坑篇

    简述 先说一下背景,之所以封装handsontable插件,是因为公司要实现在线编辑导入excel文件的功能,然后我就找到了这个功能强大的插件handsontable. 具体功能 除了handsont ...

  8. (转载)ExpandableListView 安卓二级菜单

    ExpandableListView 安卓二级菜单   ExpandableListView可以显示一个视图垂直滚动显示两级列表中的条目,这不同于列表视图(ListView).ExpandableLi ...

  9. APICloud关闭Key Building Resolve

    顶部菜单 --> 扩展 --> keybinding resolver --> toggle

  10. 浅谈Android和IOS系统的差异

    总结:事件响应级别.GPU加速.进程前后台.代码运行速度.内存管理机制. 进程管理机制.内存管理机制.cpu效率.GPU加速.事件响应级别. 1.    渲染机制不同 IOS的UI渲染采用实时优先级, ...