1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <cctype> using namespace std;
typedef long long LL; const int maxn = 33; char s[maxn][maxn], ans[maxn]; bool vis[maxn]; bool g[maxn][maxn]; int d[maxn], cnt; int L[maxn], R[maxn], U[maxn], D[maxn];
void proc(char c, int x) { if (isalpha(c) && c - 'A' != x) { g[x][c - 'A'] = true; } }
void build(int x) { for (int i = L[x]; i <= R[x]; ++i) { proc(s[U[x]][i], x); proc(s[D[x]][i], x); } for (int i = U[x]; i <= D[x]; ++i) { proc(s[i][L[x]], x); proc(s[i][R[x]], x); } }
void dfs(int dep) { if (dep == cnt) { ans[dep] = 0; puts(ans); return; } for (int i = 0; i < maxn; ++i) { if (vis[i] && d[i] == 0) { vis[i] = false; ans[dep] = 'A' + i; for (int j = 0; j < maxn; ++j) { if (g[i][j]) { --d[j]; } } dfs(dep + 1); vis[i] = true; for (int j = 0; j < maxn; ++j) { if (g[i][j]) { ++d[j]; } } } } }
int main() { freopen("frameup.in", "r", stdin); freopen("frameup.out", "w", stdout); int n, m; scanf("%d%d", &n, &m); memset(L, 0x3f, sizeof L); memset(U, 0x3f, sizeof U); for (int i = 0; i < n; ++i) { scanf("%s", s[i]); for (int j = 0; j < m; ++j) { if (isalpha(s[i][j])) { int c = s[i][j] - 'A'; vis[c] = true; L[c] = min(L[c], j); R[c] = max(R[c], j); U[c] = min(U[c], i); D[c] = max(D[c], i); } } } for (int i = 0; i < maxn; ++i) { if (vis[i]) { ++cnt; build(i); } } for (int i = 0; i < maxn; ++i) { for (int j = 0; j < maxn; ++j) { if (g[i][j] && i != j) { ++d[j]; } } } dfs(0); return 0; }
|