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. 377 Combination Sum IV 组合之和 IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  2. 365 Water and Jug Problem 水壶问题

    有两个容量分别为 x升 和 y升 的水壶以及无限多的水.请判断能否通过使用这两个水壶,从而可以得到恰好 z升 的水?如果可以,最后请用以上水壶中的一或两个来盛放取得的 z升 水.你允许:    装满任 ...

  3. Ajax动态加载数据

    前言: 1.这个随笔实现了一个Ajax动态加载的例子. 2.使用.net 的MVC框架实现. 3.这个例子重点在前后台交互,其它略写. 开始: 1.控制器ActionResult代码(用于显示页面) ...

  4. iOS 项目中的常见文件

    iOS的笔记-项目中的常见文件   新建一个项目之后,有那么多的文件,下面介绍一下主要的几个. 1.文件名 (1)AppDelegate UIApplication的代理,app收到干扰的时候,进行处 ...

  5. H5调用百度地图API获取地理位置

    <script src="http://api.map.baidu.com/api?v=2.0&ak=填入申请的AK"></script> < ...

  6. Android开发笔记(4)——MainActivity.java文件修改&布局嵌套

    笔记链接:http://www.cnblogs.com/igoslly/p/6805020.html         笔记以开发名为CoffeeOrder的app活动为线索,介绍app如何从功能设计→ ...

  7. Android基础TOP5_2:MultiAutoCompleteTextView多文本自动补全文本框

    Activity: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmln ...

  8. ajax的底层前后台交互

    为什么用ajax或者它的优点: 异步加载数据,无需切换页面 更加的用户体验,局部刷新,及时验证,操作步骤简化: 节省流量 js控制数据的加载,更加灵活多用. 底层就是XMLHttpRequest对象: ...

  9. 腾讯云 LNMP+wordpress 搭建个人网站

    折腾了好几个小时才弄好(php nginx略知一二),其实一点都不难! 以此记录一下,献给首次搭建的朋友们!! 1)准备工作:(因为个人用的ubuntu16.04 LTS系统  所以这是debian版 ...

  10. 【C++】朝花夕拾——树(开篇)

    树 ===================我是分割线====================== 1. 定义: 一些结点的集合,集合可以为空.定义树的自然方式是递归的方法. 2. 相关概念: 根(ro ...