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

题目大意:

给你一个h*w大小的矩阵,这个矩阵是由x个卡片组成,每个卡片只含有一个由一个字母组成的图案,让你找到能组成这个矩阵的所有图片摆放方案,按字典序输出。

解题思路:

这题讲道理如果给出的数据极其蛋疼的话复杂度会达到26!,而且这个复杂度无法被减小。0.0所以不知道菊苣们是怎么做的。反正我是暴力搜索过的。

其实这道题目应该算是拓扑排序的内容。但是用写拓扑排序的方式来写的话= =本彩笔不会

这道题目复杂的点在于如何建图,其实建图的方式也很简单,由于已经说明保证每个卡片上的矩形四边一定会有至少一个点,那么四条边就确定了,然后对于整个矩形,每次记录一下当前点所应该有的点,再与当前点对比,形成约束条件。

建图完成之后就是搜索就行。这道题记得每次输入的时候最好还是需要清0不然可能会wa

代码:

#include <queue>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#include <functional>
using namespace std;

typedef struct node{
    int up, down, left, right;
}Range;
const int maxn = 30;

int tol, h, w;
Range r[maxn];
vector<int> vec[maxn], pre[maxn][maxn];
char aa[maxn], m[maxn][maxn], ans[maxn];
int vis[maxn], tmp[maxn], pat[maxn][maxn];

bool judge(int len){
    int v, k;
    memset(tmp, 0, sizeof(tmp));
    for(int i = 0; i <= len; ++i){
        k = vec[ ans[i] - 'A' ].size();
        for(int j = 0; j < k; ++j){
            v = vec[ ans[i] - 'A' ][j];
            ++tmp[v];
        }
        for(int j = i + 1; j <= len; ++j){
            if(tmp[ ans[j] - 'A' ]) return false;
        }
    }
    return true;
}
void dfs(int p, int num){
    ans[num] = aa[p];
    //printf("len = %d, ans = %s
", num, ans);
    if(!judge(num)) return;
    if(num == tol - 1) {
        puts(ans);
        return;
    }
    
    for(int i = 0; i < tol; ++i){
        if(vis[i]) continue;
        vis[i] = 1; 
        dfs(i, num + 1);
        vis[i] = 0;
    }
}
void init(){
    int v, val, len;
    for(int i = 0; i < h; ++i){
        for(int j = 0; j < w; ++j){
            if(m[i][j] >= 'A' && m[i][j] <= 'Z'){
                val = m[i][j] - 'A';
                if(!vis[val]){
                    vis[val] = 1;
                    aa[tol++] = m[i][j];
                    r[val].up = i; r[val].left = j;
                    r[val].down = i; r[val].right = j;
                }else{
                    r[val].left = min(r[val].left, j);
                    r[val].down = max(r[val].down, i);
                    r[val].right = max(r[val].right, j);
                }
            }
        }
    }
    for(int i = 0; i < maxn; ++i){
        for(int j = 0; j < maxn; ++j)
            pre[i][j].clear();
    }
    for(int i = 0; i < tol; ++i){
        val = aa[i] - 'A';
        
        int a = r[val].left, b = r[val].right, c = r[val].up, d = r[val].down;
        for(int j = a; j <= b; ++j){
            pre[c][j].push_back(val);
            pre[d][j].push_back(val);
        }
        for(int j = c + 1; j < d; ++j){
            pre[j][a].push_back(val);
            pre[j][b].push_back(val);
        }
    }
    
    for(int i = 0; i < h; ++i){
        for(int j = 0; j < w; ++j){
            if(m[i][j] >= 'A' && m[i][j] <= 'Z'){
                val = m[i][j] - 'A';
                len = pre[i][j].size();
                for(int k = 0; k < len; ++k){
                    v = pre[i][j][k];
                    if(val != v && !pat[val][v]){
                        pat[val][v] = 1;
                        vec[val].push_back(v);
                    }
                }
            }
        }
    }
    sort(aa, aa + tol);
}
int main(){
    // freopen("test.in", "r+", stdin);
    // freopen("test.out", "w+", stdout);
    while(~scanf("%d", &h)){
        scanf("%d", &w);
        
        memset(m, 0, sizeof(m));
        memset(r, 0, sizeof(r));
        memset(aa, 0, sizeof(aa));
        memset(vis, 0, sizeof(vis));
        memset(pat, 0, sizeof(pat));
        for(int i = 0; i < maxn; ++i) vec[i].clear();
        
        for(int i = 0; i < h; ++i){
            for(int j = 0; j < w; ++j){
                scanf(" %c", &m[i][j]);
            }
        }
        
        tol = 0;
        init();
        for(int i = 0; i < tol; ++i){
            memset(vis, 0, sizeof(vis));
            memset(ans, 0, sizeof(ans));
            vis[i] = 1;
            dfs(i, 0);
        }
    }
    return 0;
}


原文地址:https://www.cnblogs.com/wiklvrain/p/8179440.html