poj 1128 Frame Stacking(拓扑排序)

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

题意:每张图片上面画了一些边框,给出这些边框叠在一起后的图片,求从下往上的重叠顺序,如果有多种结果的话就按照字典序输出所有结果。

题解:先构图,可以用边框的左上和右下坐标表示一个边框,若能确定A框在B框上面,那么map[B][A] = 1; ++in[A];至此,构图完成,然后就是拓扑输出序列了,依次遍历整个map,找到一个字母后便针对这个字母,求出其所在方框的范围,并找出的其范围内的其他字母,建立指向原先字母的边。对图中所有字母重复这个过程,便得到了一个有向图,并对这个有向图拓扑排序,拓扑排序需要dfs+ 回溯,先选定当前入度为0的边框,将它放入num数组,然后该边框标记为已用,并将所有与它联通的边框入度减一,再继续搜索num数组下一位置,然后就该回溯了,将当前边框标记为未用,并将所有与它联通的边框入度+1.

#include 
#include 
#define maxn 32
#define maxm 28
char str[maxn][maxn],num[maxm];
bool map[maxm][maxm];
int in[maxm],n,m,total;   //total为字母个数
struct node
{
    int x,y;
}lt[maxm];    //框框最左端
struct node2
{
    int x,y;
}rt[maxm];   //框框最右端
void BuildMap()
{
    memset(in, -1, sizeof(in));
    memset(map,false, sizeof(map));
    memset(lt, 1000000, sizeof(lt));
    memset(rt, -1, sizeof(rt));
    total=0;
    for (int i=0; irt[k].x)
            {
                rt[k].x=i;
            }
            if(j>rt[k].y)
            {
                rt[k].y=j;
            }
        }
    }
    
    //建图
    for (int i=0; ilt[i].x && xlt[i].y && yk边
                    in[k]++;
                }
            }
        }
    }
}
void DFS(int k)// 拓扑排序
{
    if(k==total)
    {
        num[k]='\0';
        printf("%s\n",num);
        return;
    }
    for (int i=0; i



你可能感兴趣的:(【图论】)