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

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-;
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 = ; i < MAXN; i++)
{
if (pos[i].l == INF) continue;
if (!vis[i] && in[i] == )
{
vis[i] = true;
for (int j = ; j < E[i].size(); j++)
in[E[i][j]]--;
ans[k] = 'A' + i;
dfs(k + , ans);
vis[i] = false;
for (int j = ; j < E[i].size(); j++)
in[E[i][j]]++;
}
}
}
int main()
{
while (scanf("%d%d", &n, &m) != EOF)
{
memset(vis, , sizeof(vis));
memset(in, , sizeof(in));
cnt = ;
for (int i = ; i < MAXN; i++)
{
E[i].clear();
pos[i].u = pos[i].l = INF, pos[i].d = pos[i].r = -INF;
}
for (int i = ; i < n; i++)
{
scanf("%s", g[i]);
for (int j = ; 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 = ; i<; 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 = ; i < ; i++)
if (pos[i].l != INF)
cnt++;
memset(vis, false, sizeof(vis));
char s[MAXN];
dfs(, s);
} }

Frame Stacking 拓扑排序 图论的更多相关文章

  1. POJ 1128 Frame Stacking (拓扑排序)

    题目链接 Description Consider the following 5 picture frames placed on an 9 x 8 array. ........ ........ ...

  2. POJ 1128 Frame Stacking 拓扑排序+暴搜

    这道题输出特别坑.... 题目的意思也不太好理解.. 就解释一下输出吧.. 它让你 从下往上输出. 如果有多种情况,按照字典序从小往大输出... 就是这个多种情况是怎么产生的呢. 下面给一组样例. 很 ...

  3. CSU 1804: 有向无环图 拓扑排序 图论

    1804: 有向无环图 Submit Page   Summary   Time Limit: 5 Sec     Memory Limit: 128 Mb     Submitted: 716    ...

  4. python 排序 拓扑排序

    在计算机科学领域中,有向图的拓扑排序是其顶点的先行排序,对于每个从顶点u到顶点v的有向边uv,在排序的结果中u都在v之前. 如果图是有向无环图,则拓扑排序是可能的(为什么不说一定呢?) 任何DAG具有 ...

  5. 图论之拓扑排序 poj1128 Frame Stacking

    题目网址 http://poj.org/problem?id=1128 思路:遍历找出每一种字母出现的最大和最小的横纵坐标,假如本应出现字母A的地方出现了字母B,那么A一定在字母B之前,这就相当于点A ...

  6. Frame Stacking(拓扑排序)

    题目链接:http://acm.tju.edu.cn/toj/showp1076.html1076.   Frame Stacking Time Limit: 1.0 Seconds   Memory ...

  7. POJ 1128 Frame Stacking(拓扑排序&#183;打印字典序)

    题意  给你一些矩形框堆叠后的鸟瞰图  推断这些矩形框的堆叠顺序  每一个矩形框满足每边都至少有一个点可见  输入保证至少有一个解 按字典序输出全部可行解 和上一题有点像  仅仅是这个要打印全部的可行 ...

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

    Description Consider the following 5 picture frames placed on an 9 x 8 array.  ........ ........ ... ...

  9. POJ1128 Frame Stacking(拓扑排序)

    题目链接:http://poj.org/problem?id=1128 题意:给你一个平面,里面有些矩形(由字母围成),这些矩形互相有覆盖关系,请从求出最底层的矩形到最上层的矩形的序列,如果存在多种序 ...

随机推荐

  1. moiezen

    这题是个随机化+二分裸题--------考场上居然没有想出来--想的出来就怪了吧 我们随机一下增加x的顺序,然后进行二分之前,看看这个x加完之后能不能更新答案,不能就不二分了.具题解所说,这个复杂度是 ...

  2. [Qt Creator 快速入门] 第9章 国际化、帮助系统和Qt插件

    一.国际化 国际化的英文表述为Internationalization,通常简写为I18N(首尾字母加中间的字符数),一个应用程序的国际化就是使该应用程序可以让其他国家的用户使用的过程. Qt支持现在 ...

  3. Linux环境下修改MySQL数据库对表名大小写不敏感

    Linux系统中MySQL对数据库名称和表名是大小写敏感的,这就导致了一些麻烦,虽然已经建立了表和数据,但因为大小写导致无法找到表. MySQL数据库对表名大小写不敏感的设置方法如下: 1.查看MyS ...

  4. javascript学习之Date对象

    前几天学习了一下date对象,于是写了一个简单的时间显示放到博客页面里(位于右上角),类似这样的效果,时:分:秒 xxxx年xx月xx日. 下面来说一下具体实现步骤. 首先,既然date是一个对象,那 ...

  5. redis 配置多个ip 解决方案

    因为在 redis 中bind 指定的ip 其实为同一网段或localhost 监听ip,在这里配置 内网其他网段或者外网多个ip 后  重启 redis 是不会成功的, 这边建议使用 折中方案,开通 ...

  6. Web前端开发与iOS终端开发的异同

    语言 前端和终端作为面向用户端的程序,有个共同特点:需要依赖用户机器的运行环境,所以开发语言基本上是没有选择的,不像后台想用什么就用什么,iOS只能用Objective-C,前端只能javascrip ...

  7. JavaScript(十四)经典的Ajax

    (function(){ //唯一向外暴露一个顶层变量 var myajax = window.myajax = {}; //作者.版本号信息 myajax.author = "maxwel ...

  8. TensorFlow OOM when allocating tensor with shape[5000,384707]

    在session范围内不要进行eval()或者convert_to_tensor()操作, 否则会造成OOM,或者报出错误:GraphDef cannot be larger than 2GB usi ...

  9. cstring 转string

    (1)CString转换为string CString cs(_T("cs")); string s; s = (LPCSTR)(CStringA)(cs); (2)string转 ...

  10. QuickClip—界面原型设计

    1.需不需要设置用户登录/注册页? QuickClip没有提供该项功能.因为本产品为单纯的移动端视频编辑软件,是一个工具类软件.而且移动端软件本就追求的是方便快捷.简单易用,本产品不需要标识使用者的身 ...