首页 > 代码库 > POJ1128 Frame Stacking 【拓扑排序】+【深搜】
POJ1128 Frame Stacking 【拓扑排序】+【深搜】
Frame Stacking
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 4074 | Accepted: 1371 |
Description
Consider the following 5 picture frames placed on an 9 x 8 array.
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.
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.
........ ........ ........ ........ .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.ABAA D.BBBB.A DDDDAD.A E...AAAA EEEEEE..
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.
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
9 8 .CCC.... ECBCBB.. DCBCDB.. DCCC.B.. D.B.ABAA D.BBBB.A DDDDAD.A E...AAAA EEEEEE..
Sample Output
EDABC
两个月前做了一半就做不下去然后扔掉了的题,今天给它重新捡了起来,总算是A掉了,综合了构图、拓扑和回溯,算是很经典的题了。
题意:每张图片上面画了一些边框,给出这些边框叠在一起后的图片,求从下往上的重叠顺序,如果有多种结果的话就按照字典序输出所有结果。题解:先构图,可以用边框的左上和右下坐标表示一个边框,若能确定A框在B框上面,那么map[B][A] = 1; ++in[A];至此,构图完成,然后就是拓扑输出序列了,这里有些难度,需要用到回溯,先选定当前入度为0的边框,将它放入ans数组,然后该边框标记为已用,并将所有与它联通的边框入度减一,再继续搜索ans数组下一位置,然后就该回溯了,将当前边框标记为未用,并将所有与它联通的边框入度+1,至此DFS完成。
#include <stdio.h> #include <string.h> #define maxn 32 #define maxm 28 char ori[maxn][maxn], ans[maxm]; int m, n, in[maxm], total; bool map[maxm][maxm]; struct Node{ int x, y; } lt[maxm]; //leftButtom struct Node2{ int x, y; } rb[maxm]; //rightTop void getMap() { int i, j, t, k, x, y; memset(map, 0, sizeof(map)); memset(in, -1, sizeof(in)); memset(lt, 0x3f, sizeof(lt)); memset(rb, -1, sizeof(rb)); for(i = total = 0; i < n; ++i) for(j = 0; j < m; ++j){ if(ori[i][j] == '.') continue; t = ori[i][j] - 'A'; if(in[t] == -1){ in[t] = 0; //indicate t exist ++total; } if(i < lt[t].x) lt[t].x = i; if(i > rb[t].x) rb[t].x = i; if(lt[t].y > j) lt[t].y = j; if(rb[t].y < j) rb[t].y = j; } for(i = 0; i < maxm; ++i){ if(in[i] == -1) continue; for(x = lt[i].x; x <= rb[i].x; ++x) for(y = lt[i].y; y <= rb[i].y; ++y){ if(x > lt[i].x && y > lt[i].y && x < rb[i].x && y < rb[i].y) continue; t = ori[x][y] - 'A'; if(t != i && !map[i][t]){ map[i][t] = true; ++in[t]; } } } } void DFS(int id) //fantastic! { if(id == total){ ans[id] = '\0'; puts(ans); return; } for(int i = 0; i < maxm; ++i){ if(in[i] == 0){ ans[id] = 'A' + i; in[i] = -1; for(int j = 0; j < maxm; ++j) if(map[i][j]) --in[j]; DFS(id + 1); in[i] = 0; for(int j = 0; j < maxm; ++j) if(map[i][j]) ++in[j]; } } } int main() { int i; while(scanf("%d%d", &n, &m) == 2){ for(i = 0; i < n; ++i) scanf("%s", ori[i]); getMap(); DFS(0); } return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。