Frame Stacking 拓扑排序 图论
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
Source
多个Case输入!
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<sstream> #include<algorithm> #include<queue> #include<deque> #include<iomanip> #include<vector> #include<cmath> #include<map> #include<stack> #include<set> #include<fstream> #include<memory> #include<list> #include<string> using namespace std; typedef long long LL; typedef unsigned long long ULL; #define MAXN 35 #define N 33 #define MOD 10000007 #define INF 1000000009 const double eps = 1e-9; const double PI = acos(-1.0); /* 1 遍历一遍图 记录四个顶点位置 2 把边框遍历一遍,如果被覆盖就 在图中加一条边(如B被C覆盖,加一条B->C) B必须在C之前放好 3 DFS 输出所有解 */ struct node { int u, d, l, r; node() { u = l = INF; d = r = -INF; } }; bool vis[MAXN]; int n, m, cnt;//n行m列 有cnt个字母 vector<int> E[MAXN]; int in[MAXN];//保存每个点的入度 node pos[MAXN];//每个字母四条边框 char g[MAXN][MAXN]; void dfs(int k, char ans[]) { if (k == cnt) { ans[k] = '\0'; printf("%s\n", ans); return; } for (int i = 0; i < MAXN; i++) { if (pos[i].l == INF) continue; if (!vis[i] && in[i] == 0) { vis[i] = true; for (int j = 0; j < E[i].size(); j++) in[E[i][j]]--; ans[k] = 'A' + i; dfs(k + 1, ans); vis[i] = false; for (int j = 0; j < E[i].size(); j++) in[E[i][j]]++; } } } int main() { while (scanf("%d%d", &n, &m) != EOF) { memset(vis, 0, sizeof(vis)); memset(in, 0, sizeof(in)); cnt = 0; for (int i = 0; i < MAXN; i++) { E[i].clear(); pos[i].u = pos[i].l = INF, pos[i].d = pos[i].r = -INF; } for (int i = 0; i < n; i++) { scanf("%s", g[i]); for (int j = 0; j < m; j++) { if (g[i][j] == '.') continue; pos[g[i][j] - 'A'].l = min(pos[g[i][j] - 'A'].l, j); pos[g[i][j] - 'A'].r = max(pos[g[i][j] - 'A'].r, j); pos[g[i][j] - 'A'].u = min(pos[g[i][j] - 'A'].u, i); pos[g[i][j] - 'A'].d = max(pos[g[i][j] - 'A'].d, i); } } for (int i = 0; i<26; i++) if (pos[i].r != INF) { memset(vis, false, sizeof(vis)); for (int j = pos[i].l; j <= pos[i].r; j++) { if (!vis[g[pos[i].u][j] - 'A'] && g[pos[i].u][j] != 'A' + i) { vis[g[pos[i].u][j] - 'A'] = true; E[i].push_back(g[pos[i].u][j] - 'A'); in[g[pos[i].u][j] - 'A']++; } } for (int j = pos[i].l; j <= pos[i].r; j++) { if (!vis[g[pos[i].d][j] - 'A'] && g[pos[i].d][j] != 'A' + i) { vis[g[pos[i].d][j] - 'A'] = true; E[i].push_back(g[pos[i].d][j] - 'A'); in[g[pos[i].d][j] - 'A']++; } } for (int j = pos[i].u; j <= pos[i].d; j++) { if (!vis[g[j][pos[i].l] - 'A'] && g[j][pos[i].l] != 'A' + i) { vis[g[j][pos[i].l] - 'A'] = true; E[i].push_back(g[j][pos[i].l] - 'A'); in[g[j][pos[i].l] - 'A']++; } } for (int j = pos[i].u; j <= pos[i].d; j++) { if (!vis[g[j][pos[i].r] - 'A'] && g[j][pos[i].r] != 'A' + i) { vis[g[j][pos[i].r] - 'A'] = true; E[i].push_back(g[j][pos[i].r] - 'A'); in[g[j][pos[i].r] - 'A']++; } } } for (int i = 0; i < 26; i++) if (pos[i].l != INF) cnt++; memset(vis, false, sizeof(vis)); char s[MAXN]; dfs(0, s); } }