拓扑排序 POJ1128 Frame Stacking

拓扑排序 POJ1128 Frame Stacking

拓扑排序:
有向无环图的拓扑排序
基本算法:
无前驱的定点优先Turpo排序算法是每一步总输出当前无前驱(入度为零)的定点。
(1)从有向图中选择一个没有前驱的顶点并输出它
(2)从网中删除该顶点,并删除从该定点出发的所有有向边和有向边链接的对应顶点
(3)重复上述步骤直到剩余网络中不再存在没有前驱的顶点

性质:
(1)剩余的点一定构成环路
(2)时间复杂度:
图:G(n,m)
用邻接表存:O(n+m)

实现:
用队列实现

本题扩展:
(1)输出所有拓扑排序
dfs:用can数组代替队列。
(a)用数组can[MAXN]记录所有的可以被选择的结点
(b)dfs过程中依据字典序选择结点
(c)将入度为零的点i设为can[i]为true

题目:
题目原文

Frame Stacking
Time Limit: 1000MS

Memory Limit: 10000K
Total Submissions: 5641

Accepted: 1971
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
Source
Southern African 2001
#include <iostream>
#include <cstdio> 
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAXN = 40;
const int INF = 0x7fffffff;

int h;
int w;
int highest;
int lowest;
int leftest;
int rightest;
int tot;
int du[MAXN];
char map[MAXN][MAXN];
bool used[30];
int used2[MAXN];
int can[MAXN];
int choose[MAXN];

void init() {
    memset(du, 0, sizeof du);
    memset(used, 0, sizeof used);
    memset(can, 0, sizeof can);
    memset(choose, 0, sizeof choose);
}

struct Link {
    int node;
    Link* next;
    void push_back(int k) {
        Link* p = new Link;
        p->node = k;
        p->next = next;
        next = p;
    }
}tu[30];

void search(char tip) {
    for (int i = 1; i <= h; ++i) 
        for (int j = 1; j <= w; ++j) 
            if (map[i][j] == tip) {
                highest = min(highest, i);
                lowest = max(lowest, i);
                leftest = min(leftest, j);
                rightest = max(rightest, j);        
            }
}

void build(char tip) {
    memset(used2, 0, sizeof used2);
    for (int i = highest; i <= lowest; ++i) {
        if (!used2[map[i][leftest]-'A'] && map[i][leftest] != tip)
            tu[tip-'A'].push_back(map[i][leftest]-'A');
        if (!used2[map[i][rightest]-'A'] && map[i][rightest] != tip)
            tu[tip-'A'].push_back(map[i][rightest]-'A');
        used2[map[i][leftest]-'A'] = used2[map[i][rightest]-'A'] = true;
    }
    for (int i = leftest; i <= rightest; ++i) {
        if (!used2[map[highest][i]-'A'] && map[highest][i] != tip)
            tu[tip-'A'].push_back(map[highest][i]-'A');
        if (!used2[map[lowest][i]-'A'] && map[lowest][i] != tip) 
            tu[tip-'A'].push_back(map[lowest][i]-'A');
        used2[map[highest][i]-'A'] = used2[map[lowest][i]-'A'] = true;
    }   
}

void dfs(int tip) {
    if (tip > tot) {
        for (int i = 1; i <= tot; ++i)
            printf("%c", choose[i] + 'A');
        printf("\n");
        return;
    } 
    for (int i = 0; i < 26; ++i) {
        if (can[i]) {
            choose[tip] = i;
            can[i] = false;
            for (Link* j = tu[i].next; j; j = j->next) {
                du[j->node]--;
                if (du[j->node] == 0)
                    can[j->node] = true;
            }
            dfs(tip + 1);
            for (Link* j = tu[i].next; j; j = j->next) {
                du[j->node]++;
                can[j->node] = false;
            }
            can[i] = true;
        }
    }
}

void Turpo() {
    for (int i = 0; i < 26; ++i)
        if (used[i])
            for (Link* j = tu[i].next; j; j = j->next)
                du[j->node]++;
    tot = 0;
    for (int i = 0; i < 26; ++i)
        if (used[i])
            ++tot;
    for (int i = 0; i < 26; ++i)
        if (used[i] && du[i] == 0)
            can[i] = true;
    dfs(1);
}

int main()
{
    while (~scanf("%d%d", &h, &w)) {
        init();
        for (int i = 1; i <= h; ++i)
            scanf("%s", &map[i][1]);
        for (int i = 0; i <= 26; ++i)
            tu[i].next = NULL;
        for (int i = 1; i <= h; ++i) 
            for (int j = 1; j <= w; ++j)
                if (map[i][j] != '.' && !used[map[i][j]-'A']) {
                    used[map[i][j] - 'A'] = true;
                    highest = INF;
                    lowest = 0;
                    leftest = INF;
                    rightest = 0;
                    search(map[i][j]);
                    build(map[i][j]);
                }
        Turpo();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值