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

题意:给你一张图,图里有很多矩形,矩形的边框用不同的字母表示,这些矩形重叠在一起,要你给出从上到下一次的顺序

思路:首先一个坑点,该顺序不唯一(如果完全没重叠就用按字典序排),所以不用队列来做拓扑排序了,这里是dfs。记录一个矩形只需要记录对角两个点就够了,然后我们看矩形的边上有没有其他字母,有的话就说明另一个字符在上面,相应邻接表置为1,然后dfs。

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<queue>
#include<cmath>
#include<string>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<iostream>
#include<algorithm>
#include<sstream>
#define ll long long
const int N=210;
const int INF=1e9;
using namespace std;
struct node{
int x1,y1,x2,y2;
}pos[30];
char mp[N][N];
int n,m,in[N],exist[30],table[30][30];
void frame(){
memset(in,0,sizeof(in));
for(int i=0;i<n;i++){ //确定边框
for(int j=0;j<m;j++){
if(mp[i][j]=='.') continue;
int x=mp[i][j]-'A';
exist[x]=1;
if(i<pos[x].x1) pos[x].x1=i;
if(j<pos[x].y1) pos[x].y1=j;
if(i>pos[x].x2) pos[x].x2=i;
if(j>pos[x].y2) pos[x].y2=j;
}
}
for(int i;i<26;i++){ //重叠计算
if(exist[i]){
for(int j=pos[i].x1;j<=pos[i].x2;j++){
int tmp=mp[j][pos[i].y1]-'A';
if(table[i][tmp]==0 && i!=tmp){
table[i][tmp]=1;
in[tmp]++;
}
tmp=mp[j][pos[i].y2]-'A';
if(table[i][tmp]==0 && i!=tmp){
table[i][tmp]=1;
in[tmp]++;
}
}
for(int j=pos[i].y1;j<=pos[i].y2;j++){
int tmp=mp[pos[i].x1][j]-'A';
if(table[i][tmp]==0 && i!=tmp){
table[i][tmp]=1;
in[tmp]++;
}
tmp=mp[pos[i].x2][j]-'A';
if(table[i][tmp]==0 && i!=tmp){
table[i][tmp]=1;
in[tmp]++;
}
}
}
}
} int cnt;
char ans[30];
void dfs(int num){
if(num==cnt){
ans[cnt]='\0';
printf("%s\n",ans);
return;
}
for(int i=0;i<26;i++){
if(exist[i] && in[i]==0){
ans[num]='A'+i;
in[i]=-1;
for(int j=0;j<26;j++){
if(table[i][j]){
in[j]--;
}
}
dfs(num+1);
in[i]=0;
for(int j=0;j<26;j++){
if(table[i][j]){
in[j]++;
}
}
}
}
}
void init(){
memset(exist,0,sizeof(exist));
memset(in,0,sizeof(in));
memset(table,0,sizeof(table));
for(int i=0;i<N;i++){
pos[i].x1=100;
pos[i].x2=-1;
pos[i].y1=100;
pos[i].y2=-1;
}
}
int main(){
while(~scanf("%d%d",&n,&m)){
init();
for(int i=0;i<n;i++){
scanf("%s",mp[i]);
}
frame();
cnt=0;
for(int i=0;i<30;i++){
if(exist[i]) cnt++;
}
dfs(0);
}
return 0;
}

POJ1128 Frame Stacking(拓扑排序+dfs)题解的更多相关文章

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

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

  2. Frame Stacking 拓扑排序 图论

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

  3. POJ 1128 Frame Stacking 拓扑排序+暴搜

    这道题输出特别坑.... 题目的意思也不太好理解.. 就解释一下输出吧.. 它让你 从下往上输出. 如果有多种情况,按照字典序从小往大输出... 就是这个多种情况是怎么产生的呢. 下面给一组样例. 很 ...

  4. ACM/ICPC 之 拓扑排序+DFS(POJ1128(ZOJ1083)-POJ1270)

    两道经典的同类型拓扑排序+DFS问题,第二题较第一题简单,其中的难点在于字典序输出+建立单向无环图,另外理解题意是最难的难点,没有之一... POJ1128(ZOJ1083)-Frame Stacki ...

  5. 拓扑排序+DFS(POJ1270)

    [日后练手](非解题) 拓扑排序+DFS(POJ1270) #include<stdio.h> #include<iostream> #include<cstdio> ...

  6. POJ1128 Frame Stacking(拓扑排序)

    题目链接:http://poj.org/problem?id=1128 题意:给你一个平面,里面有些矩形(由字母围成),这些矩形互相有覆盖关系,请从求出最底层的矩形到最上层的矩形的序列,如果存在多种序 ...

  7. 图论之拓扑排序 poj1128 Frame Stacking

    题目网址 http://poj.org/problem?id=1128 思路:遍历找出每一种字母出现的最大和最小的横纵坐标,假如本应出现字母A的地方出现了字母B,那么A一定在字母B之前,这就相当于点A ...

  8. Ordering Tasks(拓扑排序+dfs)

    Ordering Tasks John has n tasks to do. Unfortunately, the tasks are not independent and the executio ...

  9. HDU 5438 拓扑排序+DFS

    Ponds Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Sub ...

随机推荐

  1. vue中给请求到的数据对象加属性问题

    今天发现了个很奇怪的问题,我在做一个:点击列表  使点中的列表项变色的功能,而且是多个大列表项,在每个大列表项里点击切换列表项的时候不影响其他大列表项的选项. 解决思路,因为这些大列表项是请求到的数据 ...

  2. mysql清空有外键关联的表

    第一种:(不要外键约束) 手动删除外键约束: 删除表数据 第二种:(保留外键约束) SET FOREIGN_KEY_CHECKS = 0;   TRUNCATE TABLE 表名;  SET FORE ...

  3. struts2 OGNL(Object-Graph Navigation Language) 井号,星号,百分号

    1.“#”主要有三种用途: 访问OGNL上下文和Action上下文,#相当于ActionContext.getContext():可以访问这几个ActionContext中的属性. parameter ...

  4. /proc/meminfo

    /proc/meminfo  可以查看自己服务器 物理内存 注意这个文件显示的单位是kB而不是KB,1kB=1000B,但是实际上应该是KB,1KB=1024B 这个显示是不精确的,是一个已知的没有被 ...

  5. Tensorflow(一)

    一.安装Ubantu环境 下载ios 网址:http://cn.ubuntu.com/download/ 2.配合虚拟机进行安装环境 虚拟机直接百度下载即可 虚拟机采用 3.配置 4.安装中 5.安装 ...

  6. Tomcat项目部署的三种方法

    第一种方法如下:直接把我们的项目文件夹放到tomcat里面,在这里我自己做的是一个测试项目oa,如图 启动tomcat,打开浏览器,输入localhost/oa  即可打开你的文件,注意 :访问的时候 ...

  7. 连接数据库工具类DBUtil

    代码如下: import java.io.FileNotFoundException; import java.io.IOException; import java.sql.Connection; ...

  8. end=‘’

    print('----------------') a = ['aa','bb','cc','dd','ee'] for i in range(len(a)): print(i,a[i])#默认换行 ...

  9. 案例:使用scan IP无法连接数据库

    环境:Oracle RAC(11.2.0.3) 现象:通过scanIP连接数据库报错ORA-12514: ORA-12514: TNS:listener does not currently know ...

  10. html09

    1.Jquery的常用方法1)选择器2)操作节点以下的 obj 都是指 jQuery对象 1.操作样式 obj.css() :不加参数是获取节点的css样式 obj.css({"属性&quo ...