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; }
相关文章推荐
- zoj 1083 Frame Stacking
- POJ 1128 & ZOJ 1083 Frame Stacking (拓扑排序)
- ZOJ 1083 Frame Stacking(拓扑排序)
- ZOJ-1083
- POJ1083 HDU1050 ZOJ1029 UVALive2326 Moving Tables【序列+最值】
- ZOJ-1029(POJ-1083、HDU-1050) Moving Tables
- zoj1083 Frame Stacking 拓扑排序,Dfs
- TOJ 3030 ZOJ 1140 HDU 1083 Courses / 二分图
- POJ 1083 —— Moving Tables
- zoj 3792 Romantic Value(最小割下边数最小)
- zoj 3827 Information Entropy 【水题】
- ZOJ 1648 Circuit Board(线段相交判定 刘汝佳模板)
- zoj 2156
- zoj 1655 单源最短路 改为比例+最长路
- zoj 3882 博弈 *
- 1083:十->二进制转换
- ZOJ 1008 Gnome Tetravex(DFS)
- ZOJ 3791 An Easy Game (DP)
- zoj 3870 Team Formation(异或运算)
- 【DBSDFZOJ 4415】黄金拼图(乱搞)