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. vue开发环境和生产环境里面解决跨域的几种方法

    什么是跨域   跨域指浏览器不允许当前页面的所在的源去请求另一个源的数据.源指协议,端口,域名.只要这个3个中有一个不同就是跨域. 这里列举一个经典的列子: #协议跨域 http://a.baidu. ...

  2. [POJ3417]Network/闇の連鎖

    Description 传说中的暗之连锁被人们称为 Dark. Dark 是人类内心的黑暗的产物,古今中外的勇者们都试图打倒它.经过研究,你发现 Dark 呈现无向图的结构,图中有 N 个节点和两类边 ...

  3. 282 Expression Add Operators 给表达式添加运算符

    给定一个仅包含0-9的字符串和一个目标值,返回在数字之间添加了二元运算符(不是一元的) +.-或*之后所有能得到目标值的情况.例如:"123", 6 -> ["1+ ...

  4. poj2502 Subway

    思路: 需要注意的地方:一条地铁线路并不一定和样例描述的那样是直的:同一条线路上的两个站点步行可能更快. 实现: #include <iostream> #include <cstd ...

  5. String field contains invalid UTF-8 data when serializing a protocol buffer. Use the 'bytes' type if you intend to send raw bytes.

    [libprotobuf ERROR google/protobuf/wire_format.cc:1053] String field contains invalid UTF-8 data whe ...

  6. offset家族基本简介

    Offset家族简介 offset这个单词本身是--偏移,补偿,位移的意思. js中有一套方便的获取元素尺寸的办法就是offset家族: offsetWidth和offsetHight 以及offse ...

  7. 读《An Adaptable and Extensible Geometry Kernel》

    读<An Adaptable and Extensible Geometry Kernel> 利用Curiously Recurring Template Pattern替代虚函数 详细内 ...

  8. Laravel 网站项目目录结构规划

    最近在用Laravel这个PHP框架搭网站,大致了解这个框架的目录结构之后感觉学到了不少东西. 首先安装好包管理器: PHP部分当然用composer,安装在全局目录下方便一点. 前端部分,我没有选择 ...

  9. jmeter 常见问题一(url重定向)

    刚接触JMeter,想把学习过程中遇到的一些问题,记录下来因为是新接触,所以很多东西都在摸索中! 使用Badboy录制了公司一个项目的登录功能导入到JMeter后,执行场景,发现登录校验成功,但后续的 ...

  10. Getting start with dbus in systemd (03) - sd-bus.h 使用例子 (systemd version>=221)

    sd-bus.h 例子 注意: sd-dbus 是systemd提供的lib,但是这个lib,只有在systemd>v221版本后才可以使用,centos 219版本太低,所以不能使用. 参考: ...