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:
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.
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.
There will always be at least one legal ordering.
题意:给你一些重叠的字母框的俯视图,要求求出所有可能的排列顺序,从下到上。。。
分析:简单题一道,首先确定所有框所在的位置,然后扫描这些框所对应的各个格子,如果格子上的字母不一样,说明它被格子上的那个字母对应的框覆盖了,标记被覆盖的关系,最后用拓扑排序输出所有情况。。。没注意看题,被坑了一下
代码:
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; }
相关文章推荐
- Frame Up_usaco 4.4_拓扑排序
- USACO 4.4 Frame Up(拓扑排序)
- USACO 4.4 Frame Up (拓扑排序)
- usaco 4.4 Frame Up 2010.8.8
- USACO 4.4 Frame Up
- USACO4.4.3 Frame Up (frameup)
- 【USACO题库】4.4.3 Frame Up重叠的图像
- 洛谷 P1344 [USACO4.4]追查坏牛奶Pollutant Control
- BZOJ 3890 Usaco2015 Jan Meeting Time 拓扑排序
- 求割边最少的最小割(P1344 [USACO4.4]追查坏牛奶Pollutant Control)
- bzoj 1638: [Usaco2007 Mar]Cow Traffic 奶牛交通(拓扑排序?+DP)
- USACO 4.4 Pollutant Control
- BZOJ 3012: [Usaco2012 Dec]First! 字典树 拓扑排序
- USACO 4.4 Shuttle Puzzle
- 关于拓扑排序的问题-P3116 [USACO15JAN]会议时间Meeting Time
- usaco4.4
- USACO4.4.3 Frame Up (frameup)
- USACO 4.4 Pollutant Control (milk6)
- usaco frame up(所有拓扑排序的输出)
- usaco 4.4 Shuttle Puzzle(字符hash+搜索)