HDU 1128 Frame Stacking 拓扑排序

Frame Stacking
Time Limit: 1000MS      Memory Limit: 10000K
Total Submissions: 5310     Accepted: 1853

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

因为每条边都至少有一个点是可见的 所以每个方框的左上角和右下角坐标都能确定
fram[i][2]储蓄第i个方框的左上角和右下角坐标
然后遍历整个方框i 如果x在方框i上 就添加一条从i指向x的边
这样构成的图进行拓扑排序 得出的序列就符合底部在前 上方的在后
此处要求求出所有序列 so dfs求拓扑即可

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>

char ans[28];//记录拓扑排序后序列
int indegree[26];//入度
pii fram[26][2];//第i个方框的左上角&右下角坐标
char mp[35][35];//读入的图
vector<int>G[26];//进行拓扑排序的邻接链表
bool used[26];//dfs标记

int ini(int h,int w){//返回顶点个数(不同的字母个数)
    int v=0;
    fill(indegree,indegree+26,INF);//入度=INF表示图中不存在这个点
    for(int i=0;i<26;++i){
        fram[i][0]={INF,INF};//左上角x,y都是方框里最小的
        fram[i][1]={-INF,-INF};//右下角x,y都是最大的
        G[i].clear();
    }
    for(int i=0;i<h;++i)
        for(int j=0;j<w;++j)
            if(mp[i][j]!='.'){
                int index=mp[i][j]-'A';
                v+=indegree[index]==INF;
                indegree[index]=0;
                pii&leftTop=fram[index][0];
                pii&rightBelow=fram[index][1];
                leftTop.first=min(i,leftTop.first);//更新左上右下
                leftTop.second=min(j,leftTop.second);
                rightBelow.first=max(i,rightBelow.first);
                rightBelow.second=max(j,rightBelow.second);
            }
    return v;
}

inline void check(int index,int upper,bool*used){
    if(upper!=index&&!used[upper]){
        ++indegree[upper];//添加入度
        G[index].push_back(upper);//添加一条从下方->上方的边
        used[upper]=true;
    }
}

void updateIndegree(){//更新入度
    bool used[26];
    for(int index=0;index<26;++index){
        pii&leftTop=fram[index][0];
        pii&rightBelow=fram[index][1];
        if(leftTop.first==INF)
            continue;
        fill(used,used+26,false);
        //搜索方框中的其他方框
        for(int i=leftTop.first;i<=rightBelow.first;++i){
            int j=leftTop.second;
            if(mp[i][j]=='.')
                continue;
            int upper=mp[i][j]-'A';
            check(index,upper,used);//是否是其他方框 且第一次发现
            j=rightBelow.second;
            upper=mp[i][j]-'A';
            check(index,upper,used);
        }
        for(int j=leftTop.second+1;j<rightBelow.second;++j){
            int i=leftTop.first;
            if(mp[i][j]=='.')
                continue;
            int upper=mp[i][j]-'A';
            check(index,upper,used);
            i=rightBelow.first;
            upper=mp[i][j]-'A';
            check(index,upper,used);
        }
    }
}

void dfs(int index,int v){
    if(index==v){
        ans[index]='\0';
        printf("%s\n",ans);
        return;
    }
    for(int i=0;i<26;++i){
        if(indegree[i]==0&&!used[i]){
            ans[index]=i+'A';
            for(int j=0;j<G[i].size();++j)
                --indegree[G[i][j]];
            used[i]=true;
            dfs(index+1,v);
            for(int j=0;j<G[i].size();++j)
                ++indegree[G[i][j]];
            used[i]=false;
        }
    }
}

int main()
{
    //freopen("/home/lu/文档/r.txt","r",stdin);
    //freopen("/home/lu/文档/w.txt","w",stdout);
    int h,w;
    while(~scanf("%d%d",&h,&w)){
        for(int i=0;i<h;++i)
            scanf("%s",mp[i]);
        int v=ini(h,w);
        updateIndegree();
        fill(used,used+26,false);
        dfs(0,v);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值