博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ1128 Frame Stacking(拓扑排序+dfs)题解
阅读量:6668 次
发布时间:2019-06-25

本文共 3913 字,大约阅读时间需要 13 分钟。

Description

Consider the following 5 picture frames placed 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 them 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 it hides that part of the frame below. 
Viewing the stack of 5 frames we see the following. 
.CCC....ECBCBB..DCBCDB..DCCC.B..D.B.ABAAD.BBBB.ADDDDAD.AE...AAAAEEEEEE..
In what order are the frames stacked from bottom to top? The answer is EDABC. 
Your problem is to determine the order in which the frames are stacked from bottom to top given a picture of the stacked frames. Here are the rules: 
1. The width of the frame is always exactly 1 character and the sides are never shorter than 3 characters. 
2. It is possible to see at least one part of each of the four sides of a frame. A corner shows two sides. 
3. The frames will be lettered with capital letters, and no two frames will be assigned the same letter.

Input

Each input block contains the height, h (h<=30) on the first line and the width w (w<=30) on the second. A picture of the stacked frames is then given as h strings with w characters each. 
Your input may contain multiple blocks of the format described above, without any blank lines in between. All blocks in the input must be processed sequentially.

Output

Write the solution to the standard output. Give 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, each one on a separate line. There will always be at least one legal ordering for each input block. List the output for all blocks in the input sequentially, without any blank lines (not even between blocks).

Sample Input

98.CCC....ECBCBB..DCBCDB..DCCC.B..D.B.ABAAD.BBBB.ADDDDAD.AE...AAAAEEEEEE..

Sample Output

EDABC

题意:给你一张图,图里有很多矩形,矩形的边框用不同的字母表示,这些矩形重叠在一起,要你给出从上到下一次的顺序

思路:首先一个坑点,该顺序不唯一(如果完全没重叠就用按字典序排),所以不用队列来做拓扑排序了,这里是dfs。记录一个矩形只需要记录对角两个点就够了,然后我们看矩形的边上有没有其他字母,有的话就说明另一个字符在上面,相应邻接表置为1,然后dfs。

代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ll long long const int N=210;const int INF=1e9;using namespace std;struct node{ int x1,y1,x2,y2;}pos[30];char mp[N][N];int n,m,in[N],exist[30],table[30][30];void frame(){ memset(in,0,sizeof(in)); for(int i=0;i
pos[x].x2) pos[x].x2=i; if(j>pos[x].y2) pos[x].y2=j; } } for(int i;i<26;i++){ //重叠计算 if(exist[i]){ for(int j=pos[i].x1;j<=pos[i].x2;j++){ int tmp=mp[j][pos[i].y1]-'A'; if(table[i][tmp]==0 && i!=tmp){ table[i][tmp]=1; in[tmp]++; } tmp=mp[j][pos[i].y2]-'A'; if(table[i][tmp]==0 && i!=tmp){ table[i][tmp]=1; in[tmp]++; } } for(int j=pos[i].y1;j<=pos[i].y2;j++){ int tmp=mp[pos[i].x1][j]-'A'; if(table[i][tmp]==0 && i!=tmp){ table[i][tmp]=1; in[tmp]++; } tmp=mp[pos[i].x2][j]-'A'; if(table[i][tmp]==0 && i!=tmp){ table[i][tmp]=1; in[tmp]++; } } } }}int cnt;char ans[30];void dfs(int num){ if(num==cnt){ ans[cnt]='\0'; printf("%s\n",ans); return; } for(int i=0;i<26;i++){ if(exist[i] && in[i]==0){ ans[num]='A'+i; in[i]=-1; for(int j=0;j<26;j++){ if(table[i][j]){ in[j]--; } } dfs(num+1); in[i]=0; for(int j=0;j<26;j++){ if(table[i][j]){ in[j]++; } } } }}void init(){ memset(exist,0,sizeof(exist)); memset(in,0,sizeof(in)); memset(table,0,sizeof(table)); for(int i=0;i

转载于:https://www.cnblogs.com/KirinSB/p/9409097.html

你可能感兴趣的文章
使用 Gatsby.js 搭建静态博客 1 关键文件
查看>>
BASE64编码乱码问题的浅层分析与解释
查看>>
underscore中的小技巧
查看>>
RAM SSO功能重磅发布 —— 满足客户使用企业本地账号登录阿里云
查看>>
微信小程序使用二次贝塞尔曲线画波浪
查看>>
网络之旅总览
查看>>
[基础] AHK函数对象系列-绑定函数对象v2
查看>>
当Elasticsearch遇见Kafka
查看>>
2018年第40周-scala入门-工具使用
查看>>
微信小程序快速入门分享大纲
查看>>
深度介绍:
查看>>
30行代码消费腾讯人工智能开放平台提供的自然语言处理API
查看>>
微信小程序初体验,入门练手项目--通讯录,后台是阿里云服务器
查看>>
form中只有一个input时自动提交问题
查看>>
面试官:说说快速失败和安全失败是什么
查看>>
node.js中常用的fs文件系统
查看>>
你不知道的Virtual DOM(三):Virtual Dom更新优化
查看>>
Java抽象类与接口的区别
查看>>
初始化移动样式
查看>>
在vue项目中使用vuex
查看>>