POJ 1128 Frame Stacking(拓扑排序+DFS+构图)

Posted leonard-

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 1128 Frame Stacking(拓扑排序+DFS+构图)相关的知识,希望对你有一定的参考价值。

题目链接:http://poj.org/problem?id=1128

题目:

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

题意:层层覆盖,从最下面一层开始输出所有可能的覆盖顺序。

题解:先找到每个圈的每条边(每个圈都有4条边),然后判断并进行构图,构完图后就跑DFS,这里拓扑排序就像个工具,跑DFS的时候注意标记的恢复。

 1 #include <cstdio>
 2 #include <cstring>
 3 using namespace std;
 4 
 5 int n;
 6 const int N=30;
 7 bool E[N][N];
 8 char m[N][N],ans[N];
 9 int vis[N],l[N],r[N],u[N],d[N],in[N],v[N];
10 
11 void init(){
12     n=0;
13     memset(E,0,sizeof(E));
14     for(int i=0;i<30;i++){
15         l[i]=u[i]=30;
16         r[i]=d[i]=0;
17         vis[i]=in[i]=v[i]=0;
18     }
19 }
20 
21 void build(int i,int j,int k){
22     if((k+A)!=m[i][j]){
23         if(!E[k][m[i][j]-A]){
24             E[k][m[i][j]-A]=1;
25             in[m[i][j]-A]++;
26         }
27     }
28 }
29 
30 void dfs(int st,int cnt){
31     ans[cnt]=st+A;
32     v[st]=1;
33     if(cnt==n){
34         for(int i=1;i<=n;i++) printf("%c",ans[i]);
35         printf("\n");
36         v[st]=0;
37         return ;
38     }
39     int q[30],c=0;
40     for(int i=0;i<26;i++){
41         if(E[st][i]) in[i]--;
42         if(vis[i]&&!in[i]&&!v[i]) q[c++]=i;
43     }
44     for(int i=0;i<c;i++) dfs(q[i],cnt+1);
45     for(int i=0;i<26;i++) if(E[st][i]) in[i]++;
46     v[st]=0;
47 }
48 
49 int main(){
50     int h,w;
51     while(scanf("%d%d",&h,&w)!=EOF){
52         init();
53         for(int i=1;i<=h;i++) scanf("%s",m[i]+1);
54 
55         for(int i=1;i<=h;i++){
56             for(int j=1;j<=w;j++){
57                 if(m[i][j]!=.){
58                     int c=m[i][j]-A;
59                     if(!vis[c]) vis[c]=1,n++;
60                     if(i<u[c]) u[c]=i;
61                     if(i>d[c]) d[c]=i;
62                     if(j>r[c]) r[c]=j;
63                     if(j<l[c]) l[c]=j;
64                 }
65             }
66         }
67         for(int k=0;k<26;k++){
68             int i,j;
69             if(l[k]==30) continue;
70             i=u[k];
71             for(int j=l[k];j<=r[k];j++) build(i,j,k);
72             i=d[k];
73             for(j=l[k];j<=r[k];j++) build(i,j,k);
74             j=l[k];
75             for(i=u[k];i<=d[k];i++) build(i,j,k);
76             j=r[k];
77             for(i=u[k];i<=d[k];i++) build(i,j,k);
78         }
79         for(int i=0;i<26;i++){
80             if(vis[i]&&!in[i]) dfs(i,1);
81         }
82     }
83     return 0;
84 }

 

以上是关于POJ 1128 Frame Stacking(拓扑排序+DFS+构图)的主要内容,如果未能解决你的问题,请参考以下文章

POJ1128-DAG拓扑排序

ACM/ICPC 之 拓扑排序+DFS(POJ1128(ZOJ1083))

poj 1988 Cube Stacking

POJ 1988 Cube Stacking

POJ2194 Stacking Cylinders(向量旋转)

poj1988_Cube Stacking