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
題目:要求對(duì)給出的矩陣,求出它們的疊放順序。

解法:對(duì)每種字母,求出相同字母的最小區(qū)域。然后用遞歸去枚舉每一種情況。原來(lái)是打算用深搜,但后來(lái)發(fā)現(xiàn)不可以。
            AABBA
            AXXXA
            如果用深搜,對(duì)每一個(gè)搜索過(guò)的字母標(biāo)記為‘X’,表示此點(diǎn)可以被連通。則在上面的情況,先搜A的話,也可以搜出連通,但這實(shí)際是不行的。

代碼如下:


#include<stdio.h>
#include
<string.h>
#define max 31
int o, H[26], I, M[4][2= {{-10}{01}{10}{0-1}}, T[26];
char L[27], F[10000][27];
struct N
{
    
int x, y;
    
char ch;
}
P[26][100];
struct CC
{
    
int x1, y1, x2, y2, x, y;
    
char ch;
}
N[26];
int cmp(const void *a, const void *b)
{
    
return strcmp((char*)a, (char*)b);
}

int Dfs(int g, int n, int m, char s[][max], int depth)
{
    
int i, j, k, x, y;
    x 
= N[g].x;
    y 
= N[g].y;    
    
for (i = N[g].x1, k = N[g].x2, j = N[g].y1; j <= N[g].y2; j++)
    
{
        
if (s[i][j] != N[g].ch && s[i][j] != 'X')
        
{
            
return 0;
        }

        
else
        
{
            P[depth][I].x 
= i;
            P[depth][I].y 
= j;
            P[depth][I
++].ch = s[i][j];
        }

        
if (s[k][j] != N[g].ch && s[k][j] != 'X')
        
{
            
return 0;
        }

        
else
        
{
            P[depth][I].x 
= k;
            P[depth][I].y 
= j;
            P[depth][I
++].ch = s[k][j];
        }

    }

    
for (j = N[g].y1, k = N[g].y2, i = N[g].x1; i <= N[g].x2; i++)
    
{
        
if (s[i][j] != N[g].ch && s[i][j] != 'X')
        
{
            
return 0;
        }

        
else
        
{
            P[depth][I].x 
= i;
            P[depth][I].y 
= j;
            P[depth][I
++].ch = s[i][j];
        }

        
if (s[i][k] != N[g].ch && s[i][k] != 'X')
        
{
            
return 0;
        }

        
else
        
{
            P[depth][I].x 
= i;
            P[depth][I].y 
= k;
            P[depth][I
++].ch = s[i][k];    
        }

    }

    
return 1;    
}

void Cu(int g, int n, int m, int depth, char s[][max], int num)
{
    
int i, j, t;
    I 
= 0;
    t 
= Dfs(g, n, m, s, depth);
    H[depth] 
= I;
    
if (t)
    

        
        L[depth] 
= N[g].ch;
        
if (depth + 1 == num)
        
{
            
for (i = depth; i >= 0; i--)
            
{
                F[o][depth 
- i] = L[i];
            }

            F[o
++][depth + 1= '\0';
        }

        
else
        
{
            
for (j = 0; j < H[depth]; j++)
            
{
                s[P[depth][j].x][P[depth][j].y] 
= 'X';
            }

            
for (i = 0; i < num; i++)
            
{
                
if (!T[i])
                
{
                    T[i] 
= 1;                    
                    Cu(i, n, m, depth 
+ 1, s, num);                    
                    T[i] 
= 0;
                }

            }

            
for (j = 0; j < H[depth]; j++)
            
{
                s[P[depth][j].x][P[depth][j].y] 
= P[depth][j].ch;
            }

        }

    }

}

                
int main()
{
    
char s[max][max], t1[26], ch, str[max][max];
    
int i, j, k, a, b, n, m, t[26], x1, y1, x2, y2, l, h;
    
while (scanf("%d%d"&n, &m) != EOF)
    
{
        getchar();
        
for (i = 0; i < 26; i++)
        
{
            t[i] 
= 0;
        }

        
for (i = 0; i < n; i++)
        
{
            scanf(
"%s"&s[i]);
        }

        
for (i = 0, k = 0; i < n; i++)
        
{
            
for (j = 0; j < m; j++)
            
{
                
if (s[i][j] != '.' && t[s[i][j] - 'A' ] == 0)
                
{
                    t[s[i][j] 
- 'A'= 1;
                    x1 
= 100;
                    y1 
= 100;
                    x2 
= 0;
                    y2 
= 0;
                    
for (l = 0; l < n; l++)
                    
{
                        
for (h = 0; h < m; h++)
                        
{
                            
if (s[l][h] == s[i][j])
                            
{
                                
if (l < x1)
                                
{
                                    x1 
= l;
                                }

                                
if (h < y1)
                                
{
                                    y1 
= h;
                                }

                                
if (x2 < l)
                                
{
                                    x2 
= l;
                                }

                                
if (y2 < h)
                                
{
                                    y2 
= h;
                                }

                            }

                        }

                    }

                    N[k].x 
= i;
                    N[k].y 
= j;
                    N[k].x1 
= x1;
                    N[k].x2 
= x2;
                    N[k].y1 
= y1;
                    N[k].ch 
= s[i][j];
                    N[k
++].y2 = y2;                                    
                }

            }

        }

        o 
= 0;
        
for (i = 0; i < k; i++)
        
{
            
for (j = 0; j < n; j++)
            
{
                strcpy(str[j], s[j]);
            }

            T[i] 
= 1;
            Cu(i, n, m, 
0, str, k);
            T[i] 
= 0;
        }

        qsort(F, o, 
sizeof(char* 27, cmp);
        
for (i = 0; i < o; i++)puts(F[i]);
    }

    
return 0;
}