您的位置:首页 > 其它

ZOJ 1083 Frame Stacking

2012-11-12 14:43 232 查看
FrameStacking
TimeLimit:2SecondsMemoryLimit:65536KB

Considerthefollowing5pictureframesplacedonan9x8array.

Nowplacethemontopofoneanotherstartingwith1atthebottomandendingupwith5ontop.Ifanypartofaframecoversanotherithidesthatpartoftheframebelow.

Viewingthestackof5framesweseethefollowing.

Inwhatorderaretheframesstackedfrombottomtotop?TheanswerisEDABC.

Yourproblemistodeterminetheorderinwhichtheframesarestackedfrombottomtotopgivenapictureofthestackedframes.Herearetherules:

1.Thewidthoftheframeisalwaysexactly1characterandthesidesarenevershorterthan3characters.

2.Itispossibletoseeatleastonepartofeachofthefoursidesofaframe.Acornershowstwosides.

3.Theframeswillbeletteredwithcapitalletters,andnotwoframeswillbeassignedthesameletter.

INPUTDATA

Eachinputblockcontainstheheight,h(h<=30)onthefirstlineandthewidthw(w<=30)onthesecond.Apictureofthestackedframesisthengivenashstringswithwcharacterseach.

Exampleinput:

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

Yourinputmaycontainmultipleblocksoftheformatdescribedabove,withoutanyblanklinesinbetween.Allblocksintheinputmustbeprocessedsequentially.

OUTPUTDATA

Writethesolutiontothestandardoutput.Givethelettersoftheframesintheordertheywerestackedfrombottomtotop.Iftherearemultiplepossibilitiesforanordering,listallsuchpossibilitiesinalphabeticalorder,eachoneonaseparateline.Therewillalwaysbeatleastonelegalorderingforeachinputblock.Listtheoutputforallblocksintheinputsequentially,withoutanyblanklines(notevenbetweenblocks).

ExampleOutput:

EDABC

//确定每个图形的4个顶点、然后就可以确定它被几个图覆盖了
//拓扑排序至于输出所有情况的话、dfs搞定(不过要按字母序编号噢)

#include<iostream>
#include<stdio.h>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<math.h>
#include<string.h>
#include<algorithm>
usingnamespacestd;
vector<int>v[28];
structID
{
intx,y;
};
structnode
{
IDlt;
IDrb;
charc;
booloperator<(constnode&b)const
{
returnc<b.c;
}
}rc[28];
boolbr[28];
boolbin[28][28];
intin[28];
intid[128];
charpt[28];
charmap[33][33];
intk;

voiddfs(intdp)
{
if(dp==k)
{
pt[dp]='\0';
printf("%s\n",pt);
return;
}
intl,i,j;
for(i=0;i<k;i++)
{
if(br[i])continue;
if(in[i]==0)
{
pt[dp]=rc[i].c;
l=v[i].size();
for(j=0;j<l;j++)
in[v[i][j]]--;
br[i]=1;
dfs(dp+1);
br[i]=0;
for(j=0;j<l;j++)
in[v[i][j]]++;
}
}
}
intmain()
{

inth,w;
inti,j;
while(scanf("%d%d",&h,&w)!=EOF)
{
getchar();
inttp=0;
memset(br,0,sizeof(br));
memset(in,0,sizeof(in));
memset(bin,0,sizeof(bin));
for(k=i=0;i<h;getchar(),i++)
for(j=0;j<w;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='.')continue;
if(!br[map[i][j]-'A'])
{
br[map[i][j]-'A']=1;
rc[k].c=map[i][j];
rc[k].lt.x=i;rc[k].lt.y=j;
rc[k].rb.x=i;rc[k].rb.y=j;
id[rc[k].c]=k;
k++;
}
else
{
tp=id[map[i][j]];
if(i<rc[tp].lt.x)rc[tp].lt.x=i;
if(i>rc[tp].rb.x)rc[tp].rb.x=i;
if(j<rc[tp].lt.y)rc[tp].lt.y=j;
if(j>rc[tp].rb.y)rc[tp].rb.y=j;
}
}
sort(rc,rc+k);
for(i=0;i<k;i++)
id[rc[i].c]=i;
intx,y;
for(i=0;i<k;i++)
{
x=rc[i].lt.x;
for(y=rc[i].lt.y;y<=rc[i].rb.y;y++)
if(map[x][y]!=rc[i].c)
{
tp=id[map[x][y]];
if(bin[i][tp])continue;
v[i].push_back(tp);
in[tp]++;
bin[i][tp]=1;
}
x=rc[i].rb.x;
for(y=rc[i].lt.y;y<=rc[i].rb.y;y++)
if(map[x][y]!=rc[i].c)
{
tp=id[map[x][y]];
if(bin[i][tp])continue;
v[i].push_back(tp);
in[tp]++;
bin[i][tp]=1;
}
y=rc[i].lt.y;
for(x=rc[i].lt.x;x<=rc[i].rb.x;x++)
if(map[x][y]!=rc[i].c)
{
tp=id[map[x][y]];
if(bin[i][tp])continue;
v[i].push_back(tp);
in[tp]++;
bin[i][tp]=1;
}
y=rc[i].rb.y;
for(x=rc[i].lt.x;x<=rc[i].rb.x;x++)
if(map[x][y]!=rc[i].c)
{
tp=id[map[x][y]];
if(bin[i][tp])continue;
v[i].push_back(tp);
in[tp]++;
bin[i][tp]=1;
}
}
memset(br,0,sizeof(br));
dfs(0);
for(i=0;i<k;i++)v[i].clear();
}
return0;
}


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: