ZOJ 1083 Frame Stacking(拓扑排序)

来源:互联网 发布:淘宝开店认证是什么 编辑:程序博客网 时间:2024/06/05 07:03

Frame Stacking

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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.


INPUT DATA

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.


Example input:

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

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 DATA

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).


Example Output:

EDABC 


Source: South Africa 2001


思路:拓扑排序即可,要实现拓扑序列,需要事先构造关系矩阵。


代码:

#include <stdio.h>#include <string.h>struct corner{int x1,y1;int x2,y2;corner() { x1 = 40, y1 = 40, x2 = -40, y2 = -40; }};char gg[30][30];//保存原图 char solution[26];//保存拓扑序列 char g[26][26];//拓扑排序的关系矩阵 char g1[26][26];//标记已经统计的边 int number[26];//保存各字母的入度 int used[26];//标记出现的字母 void addEdge(int i, int j, int &edges){if(g1[i][j]==0)//该边还没有被统计过 {g1[i][j] = 1;g[i][++edges] = j;//增加一条边i->j number[j] ++;//结点j作为子结点出现的次数 }}void topSort(int depth, int count)//depth表示搜索的深度,也是第depth个拓扑序,count总的结点个数 {int i, j, edges;if(depth>=count){for(i=0; i<count; i++)printf("%c", solution[i]+'A');printf("\n");return;}for(i=0; i<26; i++)if (used[i]) if (number[i]==0)//该结点不是其他结点的子结点,即入度为0 { solution[depth++] = i;//保存第depth拓扑序的结果 number[i]--;//与其相邻的结点,入度-1 edges = g[i][0];for(j=1; j<=edges; j++)number[g[i][j]]--;//递归搜索下一个 topSort(depth, count);//恢复现场 for(j=1; j<=edges; j++)number[g[i][j]]++;number[i] ++;depth --;}}int build(corner frame[]) {memset(g, 0, sizeof(g));memset(g1, 0, sizeof(g1));memset(number, 0, sizeof(number));int i, j;int count = 0;//框架的数量 for(i=0; i<26; i++)if(used[i]){count++;int edges = 0;int x1 = frame[i].x1;int y1 = frame[i].y1;int x2 = frame[i].x2;int y2 = frame[i].y2;for(j=x1; j<=x2; j++)//该框架的竖直方向 {if(gg[j][y1]!=i) addEdge(i, gg[j][y1], edges);if(gg[j][y2]!=i)addEdge(i, gg[j][y2], edges);}for(j=y1; j<=y2; j++)//该框架的水平方向 {if(gg[x1][j]!=i)addEdge(i, gg[x1][j], edges);if(gg[x2][j]!=i)addEdge(i, gg[x2][j], edges);}g[i][0] = edges;//用g[i][0]保存从i出发的关系个数 }return count;}int main(){int i, j;int n, m;char line[30], c;while (scanf("%d", &n)!=EOF){memset(used, 0, sizeof(used));scanf("%d\n", &m);corner frame[26];for(i=0; i<n; i++) {gets(line);for(j=0; j<m; j++){c = line[j] - 'A';gg[i][j] = c;//gg[][]保存整个图 if(c!=('.'-'A'))//遇到字母 {used[c] = 1;//存在字母c //扩展该字母出现的边界,注意是一个框架是一个矩阵 /*左上角*/ if(frame[c].x1>i)frame[c].x1 = i;if(frame[c].y1>j)frame[c].y1 = j;/*右下角*/ if(frame[c].x2<i)frame[c].x2 = i;if(frame[c].y2<j)frame[c].y2 = j;}}}int count = build(frame);topSort(0, count);}return 0;}

原创粉丝点击