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
题意:给一个若干个矩形叠在一起的图形,要求字典序输出所有可能的图形的叠放顺序。思路:拓扑排序,因为四条边每条至少有一格是露出来的,所以可以预处理每个矩形的边界,于是可以进行建边,最后拓扑排序输出用DFS代替以往的BFS。
# include <iostream> # include <cstdio> # include <cstring> # include <algorithm> using namespace std; char s[38][38], now, ans[28]; int b[28][4], n, m; int dx[4] = {0,1,0,-1}; int dy[4] = {1,0,-1,0}; int vis[28], in[28]; int v[28][28], q[300], tx, ty, tot; void init() { tot = 0; memset(b, 0, sizeof(b)); memset(vis, 0, sizeof(vis)); memset(in, 0, sizeof(in)); memset(ans, 0, sizeof(ans)); memset(v, 0, sizeof(v)); for(int i=0; i<26; ++i) b[i][3] = b[i][2] = 1e9; for(int i=0; i<n; ++i) { for(int j=0; j<m; ++j) { if(s[i][j] == '.') continue; int tmp = s[i][j] - 'A'; b[tmp][3] = min(b[tmp][3], i);//上界 b[tmp][2] = min(b[tmp][2], j);//左界 b[tmp][1] = max(b[tmp][1], i);//下界 b[tmp][0] = max(b[tmp][0], j);//右界 } } } void dfs(int x, int y, int cur)//dfs建边。 { int nxt; if(((cur%2==0)?y:x) == b[now-'A'][cur]) nxt = (cur+1)%4; else nxt = cur; int nx = x + dx[nxt]; int ny = y + dy[nxt]; if(nx == tx && ny == ty) return; if(s[nx][ny] != now) if(++v[now-'A'][s[nx][ny]-'A'] == 1) ++in[s[nx][ny]-'A']; dfs(nx, ny, nxt); } void dfs2(int pos) { if(pos == tot) { printf("%s\n",ans); return; } for(int i=0; i<26; ++i) { if(!vis[i]) continue; if(in[i] == 0) { --in[i]; ans[pos] = i+'A'; for(int j=0; j<26; ++j) if(v[i][j]) --in[j]; dfs2(pos+1); for(int j=0; j<26; ++j) if(v[i][j]) ++in[j]; ++in[i]; } } } int main() { while(~scanf("%d%d",&n,&m)) { for(int i=0; i<n; ++i) scanf("%s",s[i]); init(); for(int i=0; i<n; ++i) { for(int j=0; j<m; ++j) { if(s[i][j]!='.' && !vis[s[i][j]-'A']) { ++tot;//出现的字母种数。 vis[s[i][j]-'A'] = 1; now = s[i][j]; tx = i, ty = j; dfs(i,j,0); } } } dfs2(0); } return 0; }