ZOJ 1083 Frame Stacking(拓扑排序)

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 
#include 

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; ii)	frame[c].x1 = i;
					if(frame[c].y1>j)	frame[c].y1 = j;
					/*右下角*/ 
					if(frame[c].x2

你可能感兴趣的:(常用模板)