您的位置:首页 > 其它

USACO 4.4 Frame Up (拓扑排序)

2014-02-26 00:02 281 查看
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


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

不做题完全不知道这些知识点怎么用,看了题解才搞清楚题目是个什么意思,第一次写拓扑排序,感觉写的很烂,幸好题目比较简单

wa2次过了,算是新年开门红咯,马上要去找实习了,研究生生活说来也是搞笑...

今天第一天实训,开始将nginx,感觉好麻烦噢,配个流媒体一直出问题,实训这么紧,unity3d也没时间看啊,好烦

/*

ID: hubiao cave

PROG: frameup

LANG: C++

*/

#include<iostream>
#include<fstream>
#include<string>
#include<set>
#include<cstring>

using namespace std;

struct Letter
{
int lx,ly;
int rx,ry;
};

Letter lets[26];
char map[31][31];
bool con[26][26];
int  inweight[26];
bool used[26];
set<string> ss;
string str;
int depth;
void dfs(int);
int main()

{

ifstream fin("frameup.in");

ofstream fout("frameup.out");
int w,h;
fin>>h>>w;
for(int i=1;i<=h;i++)
for(int j=1;j<=w;j++)
{
fin>>map[i][j];
if(map[i][j]!='.')
{
int x=map[i][j]-'A';
if(lets[x].lx==0)
lets[x].lx=j;
else
lets[x].lx=min(lets[x].lx,j);
lets[x].rx=max(lets[x].rx,j);
lets[x].ly=max(lets[x].ly,i);
if(lets[x].ry==0)
lets[x].ry=i;
else
lets[x].ry=min(lets[x].ry,i);
}
}

for(int i=0;i<26;i++)
{
if(i==4)
i=4;
if(lets[i].lx)
{
depth++;
memset(used,0,sizeof(used));
for(int m=lets[i].ry;m<=lets[i].ly;m++)
{
if(map[m][lets[i].lx]-'A'!=i)
{
con[i][map[m][lets[i].lx]-'A']=true;
if(!used[map[m][lets[i].lx]-'A'])
{
inweight[map[m][lets[i].lx]-'A']++;
used[map[m][lets[i].lx]-'A']=true;
}
}
if(map[m][lets[i].rx]-'A'!=i)
{
con[i][map[m][lets[i].rx]-'A']=true;
if(!used[map[m][lets[i].rx]-'A'])
{
inweight[map[m][lets[i].rx]-'A']++;
used[map[m][lets[i].rx]-'A']=true;
}
}
}
for(int m=lets[i].lx+1;m<=lets[i].rx-1;m++)
{
if(map[lets[i].ry][m]-'A'!=i)
{
con[i][map[lets[i].ry][m]-'A']=true;
if(!used[map[lets[i].ry][m]-'A'])
{
inweight[map[lets[i].ry][m]-'A']++;
used[map[lets[i].ry][m]-'A']=true;
}
}
if(map[lets[i].ly][m]-'A'!=i)
{
con[i][map[lets[i].ly][m]-'A']=true;
if(!used[map[lets[i].ly][m]-'A'])
{
inweight[map[lets[i].ly][m]-'A']++;
used[map[lets[i].ly][m]-'A']=true;
}
}
}
}
}
memset(used,0,sizeof(used));
dfs(1);
for(set<string>::iterator it=ss.begin();it!=ss.end();it++)
{
fout<<*it<<endl;
}

return 0;

}

void dfs(int s)
{

for(int i=0;i<26;i++)
{
if(lets[i].lx&&inweight[i]==0&&!used[i])
{
for(int j=0;j<26;j++)
{
if(con[i][j])
inweight[j]--;
}
str+=('A'+i);
used[i]=true;
dfs(s+1);
str.erase(str.length()-1);
used[i]=false;
for(int j=0;j<26;j++)
{
if(con[i][j])
inweight[j]++;
}
}
}
if(s==depth+1)
ss.insert(str);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: