POJ 1128 Frame Stacking (topological sort & #183; print dictionary order)

Source: Internet
Author: User

Test instructions gives you some bird's-eye view of the stacking of rectangles. The stacking order of these rectangles each rectangle satisfies at least one point on each side of the visible input to ensure that at least one solution is output in dictionary order for all feasible solutions

And the previous question is a bit like just this to print all of the feasible plan to build or similar because each of the four sides of the rectangle is a little visible, so the coordinates of the upper and lower left corner of each rectangle box are able to be determined and then a rectangle has other characters to make this rectangle box corresponding character and that other character to establish a less than the relationship Because you want to print a scenario, you need to use DFS to sort each selection once for each choice when there are multiple points in the 0.

#include <cstdio> #include <cstring>using namespace std;const int N = 50;char Ans[n], g[n][n], Tp[n][n];int x1[  N], Y1[n], x2[n], y2[n];//(X1,Y1) is the upper-left coordinate of the corresponding letter (x2,y2) is lower right int in[n], n;void addtopo (int i, int j, int c) {int t = G[i][j]    -' A ';        if (t! = C &&!tp[c][t]) {++in[t];    Tp[c][t] = 1;        }}void build () {memset (TP, 0, sizeof (TP)),//tp[i][j] = 1 indicates a relationship with I < J for (int c = n = 0; c <; ++c) {        if (In[c] < 0) continue;            for (int i = x1[c]; I <= x2[c]; ++i) {Addtopo (I, y1[c], c);        Addtopo (i, y2[c], c);            } for (int j = y1[c]; J <= Y2[c]; ++j) {Addtopo (x1[c], J, c);        Addtopo (X2[c], J, c);        } ++n;//Statistics How many characters appear}}void toposort (int k) {if (k = = N) {ans[k] = 0;        Puts (ans);    Return }//From the point of entry to 0 to ensure the ascending for (int i = 0; i <; ++i) {if (in[i] = = 0) {Ans[k] = i + ' A ';  This one places I          In[i] =-1;            for (int j = 0; J < ++j) if (Tp[i][j])--in[j]; Toposort (k + 1); Find the next in[i] = 0;        Backtracking for (int j = 0; J < ++j) if (Tp[i][j]) ++in[j];    }}}int Main () {int h, W, C;            while (~SCANF ("%d%d", &h, &w)) {for (int i = 0; i <; ++i) {x1[i] = y1[i] = N;        X2[i] = Y2[i] = 0;        } memset (in,-1, sizeof (in));            for (int i = 0; i < H; ++i) {scanf ("%s", G[i]);                for (int j = 0; J < W; ++j) {if ((c = g[i][j]-' A ') < 0) continue;//g[i][j] = '. '                if (I < x1[c]) x1[c] = i;                if (i > X2[c]) x2[c] = i;                if (J < y1[c]) y1[c] = j;                if (J > Y2[c]) y2[c] = j;  In[c] = 0;        The occurrence of the letter in initial is 0 otherwise-1}} build ();    Toposort (0); } return 0;}

Frame Stacking

Description

Consider the following 5 picture frames placed in 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 is the frames stacked from bottom to top? The answer is EDABC.

Your problem is to determine the order in which the frames be stacked from bottom to top given a picture of the stacked F Rames. Here is 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 and sides.

3. The frames is lettered with capital letters, and No, and no, frames would 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 are then given as h strings with w characters each.
Your input may contain multiple blocks of the format described above, without a blank lines in between. All blocks in the input must is 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 is multiple possibilities for the ordering, list all such possibilities in alphabetical order, each one on a Sepa Rate line. There always is at the least one legal ordering for each input block. List the output for all blocks in the input sequentially, without a blank lines (not even between blocks).

Sample Input

98.CCC .... ECBCBB. Dcbcdb. DCCC. B.. D.b.abaad. BBBB. Addddad. Ae... Aaaaeeeeee.

Sample Output

Edabc

POJ 1128 Frame Stacking (topological sort & #183; print dictionary order)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.