POJ1128 Frame Stacking(拓扑排序+dfs)题解

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

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


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

思路:首先一个坑点,该顺序不唯一(如果完全没重叠就用按字典序排),所以不用队列来做拓扑排序了,这里是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;ipos[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

你可能感兴趣的:(POJ1128 Frame Stacking(拓扑排序+dfs)题解)