您的位置:首页 > 其它

usaco 4.4 Frame Up(拓扑排序)

2012-09-20 20:57 246 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: