usaco 4.4 Frame Up(拓扑排序)

Frame Up

Consider the following five picture frames shown 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 all five picture frames 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 frame, it hides that part of the frame below. Viewing the stack of five frames we see the following.

           .CCC...
           ECBCBB..
           DCBCDB..
           DCCC.B..
           D.B.ABAA
           D.BBBB.A
           DDDDAD.A
           E...AAAA
           EEEEEE..

Given a picture like this, determine the order of the frames stacked from bottom to top.

Here are the rules for this challenge:

  • The width of the frame is always exactly 1 character and the sides are never shorter than 3 characters.
  • It is possible to see at least one part of each of the four sides of a frame. A corner is part of two sides.
  • The frames will be lettered with capital letters, and no two frames will be assigned the same letter.

PROGRAM NAME: frameup

INPUT FORMAT

Line 1:Two space-separated integers: the height H (3 <= H <=30) and the width W (3 <= W <= 30).
Line 2..H+1:H lines, each with a string W characters wide.

SAMPLE INPUT (file frameup.in)

9 8
.CCC....
ECBCBB..
DCBCDB..
DCCC.B..
D.B.ABAA
D.BBBB.A
DDDDAD.A
E...AAAA
EEEEEE..

OUTPUT FORMAT

Print 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 -- on successive lines. There will always be at least one legal ordering.

SAMPLE OUTPUT (file frameup.out)

EDABC

题意:给你一些重叠的字母框的俯视图,要求求出所有可能的排列顺序,从下到上。。。

分析:简单题一道,首先确定所有框所在的位置,然后扫描这些框所对应的各个格子,如果格子上的字母不一样,说明它被格子上的那个字母对应的框覆盖了,标记被覆盖的关系,最后用拓扑排序输出所有情况。。。没注意看题,被坑了一下

代码:

/*
ID: 15114582
PROG: frameup
LANG: C++
*/
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int l[33],r[33],t[33],b[33],v[33],s[33];
char map[33][33],ok[33][33],w[333];
int i,j,k,n,m,num;
void father(int i,int j)
{
    if(i==j||ok[i][j])return;
    ok[i][j]=1;
    ++s[j];
}
void dfs(int k)
{
    if(k>=num)
    {
        w[k]='\0';
        puts(w);
        return;
    }
    for(int i=0,j;i<26;++i)
        if(v[i]&&!s[i])
        {
            v[i]=0;
            w[k]=i+'A';
            for(j=0;j<26;++j)
                if(ok[i][j])--s[j];
            dfs(k+1);
            for(j=0;j<26;++j)
               if(ok[i][j])++s[j];
            v[i]=1;
        }
}
int main()
{
    freopen("frameup.in","r",stdin);
    freopen("frameup.out","w",stdout);
    while(~scanf("%d%d",&n,&m))
    {
        for(i=0;i<n;++i)
            scanf("%s",map[i]);
        memset(ok,0,sizeof(ok));
        for(i=0;i<26;++i)s[i]=v[i]=r[i]=b[i]=0,l[i]=t[i]=33;
        for(i=0;i<n;++i)
            for(j=0;j<m;++j)
                if(map[i][j]!='.')
                {
                    k=map[i][j]-'A';
                    v[k]=1;
                    l[k]=min(l[k],j);
                    r[k]=max(r[k],j);
                    t[k]=min(t[k],i);
                    b[k]=max(b[k],i);
                }
        for(num=k=0;k<26;++k)
            if(v[k])
            {
                ++num;
                for(i=t[k];i<=b[k];++i)
                {
                    father(k,map[i][l[k]]-'A');
                    father(k,map[i][r[k]]-'A');
                }
                for(j=l[k];j<=r[k];++j)
                {
                    father(k,map[t[k]][j]-'A');
                    father(k,map[b[k]][j]-'A');
                }
            }
        dfs(0);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值