poj 1128 Frame Stacking(拓扑排序)

来源:互联网 发布:阿里云主机禁止ip访问 编辑:程序博客网 时间:2024/06/06 05:05

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.ABAAD.BBBB.ADDDDAD.AE...AAAAEEEEEE..



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

98.CCC....ECBCBB..DCBCDB..DCCC.B..D.B.ABAAD.BBBB.ADDDDAD.AE...AAAAEEEEEE..

Sample Output

EDABC

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

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

#include <stdio.h>#include <string.h>#define maxn 32#define maxm 28char 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; i<n; i++)    {        for (int j=0; j<m; j++)        {            if(str[i][j]=='.')            {                continue;            }            int k=str[i][j]-'A';   //转换为数字            if(in[k]==-1)            {                in[k]=0;   //存在该字母                total++;    //个数++            }                        //确定框框的范围            if(i<lt[k].x)            {                lt[k].x=i;            }            if(j<lt[k].y)            {                lt[k].y=j;            }            if(i>rt[k].x)            {                rt[k].x=i;            }            if(j>rt[k].y)            {                rt[k].y=j;            }        }    }        //建图    for (int i=0; i<maxm; i++)    {        if(in[i]==-1)//没有该字母        {            continue;        }        for (int x=lt[i].x; x<=rt[i].x; x++)        {            for (int y=lt[i].y; y<=rt[i].y; y++)            {                if(x>lt[i].x && x<rt[i].x && y>lt[i].y && y<rt[i].y)  //自考虑框框边缘                {                    continue;                }                int k=str[x][y]-'A';                if(k!=i && !map[i][k])                {                    map[i][k]=true;  //图中存在i->k边                    in[k]++;                }            }        }    }}void DFS(int k)// 拓扑排序{    if(k==total)    {        num[k]='\0';        printf("%s\n",num);        return;    }    for (int i=0; i<maxm; i++) //从最小的字母开始搜,因此肯定是字典序    {        if(in[i]==0)        {            num[k]=i+'A';            in[i]=-1;            for (int j=0; j<maxm; j++)            {                if(map[i][j])                {                    in[j]--;                }            }            DFS(k+1);                        //回溯            in[i]=0;            for (int j=0; j<maxm; j++)            {                if(map[i][j])                {                    in[j]++;                }            }        }    }}int main(){    while (scanf("%d%d",&n,&m)!=EOF)    {        for (int i=0; i<n; i++)        {            scanf("%s",str[i]);        }        BuildMap();        DFS(0);    }    return 0;}



0 0
原创粉丝点击