Frame Stacking 拓扑排序 图论
Description
........ ........ ........ ........ .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
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
Sample Input
9
8
.CCC....
ECBCBB..
DCBCDB..
DCCC.B..
D.B.ABAA
D.BBBB.A
DDDDAD.A
E...AAAA
EEEEEE..
Sample Output
EDABC
Source
#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 拓扑排序 图论的更多相关文章
- POJ 1128 Frame Stacking (拓扑排序)
题目链接 Description Consider the following 5 picture frames placed on an 9 x 8 array. ........ ........ ...
- POJ 1128 Frame Stacking 拓扑排序+暴搜
这道题输出特别坑.... 题目的意思也不太好理解.. 就解释一下输出吧.. 它让你 从下往上输出. 如果有多种情况,按照字典序从小往大输出... 就是这个多种情况是怎么产生的呢. 下面给一组样例. 很 ...
- CSU 1804: 有向无环图 拓扑排序 图论
1804: 有向无环图 Submit Page Summary Time Limit: 5 Sec Memory Limit: 128 Mb Submitted: 716 ...
- python 排序 拓扑排序
在计算机科学领域中,有向图的拓扑排序是其顶点的先行排序,对于每个从顶点u到顶点v的有向边uv,在排序的结果中u都在v之前. 如果图是有向无环图,则拓扑排序是可能的(为什么不说一定呢?) 任何DAG具有 ...
- 图论之拓扑排序 poj1128 Frame Stacking
题目网址 http://poj.org/problem?id=1128 思路:遍历找出每一种字母出现的最大和最小的横纵坐标,假如本应出现字母A的地方出现了字母B,那么A一定在字母B之前,这就相当于点A ...
- Frame Stacking(拓扑排序)
题目链接:http://acm.tju.edu.cn/toj/showp1076.html1076. Frame Stacking Time Limit: 1.0 Seconds Memory ...
- POJ 1128 Frame Stacking(拓扑排序·打印字典序)
题意 给你一些矩形框堆叠后的鸟瞰图 推断这些矩形框的堆叠顺序 每一个矩形框满足每边都至少有一个点可见 输入保证至少有一个解 按字典序输出全部可行解 和上一题有点像 仅仅是这个要打印全部的可行 ...
- POJ1128 Frame Stacking(拓扑排序+dfs)题解
Description Consider the following 5 picture frames placed on an 9 x 8 array. ........ ........ ... ...
- POJ1128 Frame Stacking(拓扑排序)
题目链接:http://poj.org/problem?id=1128 题意:给你一个平面,里面有些矩形(由字母围成),这些矩形互相有覆盖关系,请从求出最底层的矩形到最上层的矩形的序列,如果存在多种序 ...
随机推荐
- $Hdu1381\ Crazy\ Search$
前置芝士 :string 的 基本用法 string s = "hello world" ; string tmp(s,0,5) ; cout << tmp <& ...
- Java多线程(一) Thread和 Runnable
http://www.cnblogs.com/lwbqqyumidi/p/3804883.html 1.继承Thread 2.实现Runnable接口 public class MyRunnable ...
- [Usaco2018 Open]Disruption
Description Farmer John自豪于他所经营的交通发达的的农场.这个农场是由N块牧场(2≤N≤50,000)组成的,N-1条双向道路将它们连接起来,每一条道路的都为一单位长度.Farm ...
- 数据传递-------@PathVariable
package com.wh.handler; /** * 通过@PathVariable可以绑定占位符参数到方法参数中,例如 * @PathVariable("userId") ...
- FCC 基础JavaScript 练习6
1.对象和数组很相似,数组是通过索引来访问和修改数据,对象是通过属性来访问和修改数据的, 对象适合用来存储结构化数据,就和真实世界的对象一模一样,比如一只猫. 任务 创建一个叫做myDog的对象,它里 ...
- AngularJs与Java Web服务器交互
AngularJs是Google工程师研发的产品,它的强大之处不是几句话就能描述的,只有真正使用过的人才能体会到,笔者准备在这篇文章中,以一个简单的登录校验的例子说明如何使用AngularJs和Web ...
- Apache ab使用指南
Apache ab使用图例: 其中比较重要的两个指标要特别注意: Requests per second:表示平均每秒事务数,相当于LR的TPS Time per second:用户请求平均响应时间和 ...
- jdbc 实现分页
jdbc 实现分页,的实现 原理这个就不介绍了.. 总之是用jdbc 的游标移动 package com.sp.person.sql.util; import java.sql.Connection; ...
- HDU_1074_Doing Homework_状态压缩dp
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1074 Doing Homework Time Limit: 2000/1000 MS (Java/Othe ...
- 搭建FileZilla
FileZilla是C/S架构的,有服务端和客户端 客户端下载地址https://www.filezilla.cn/download/client 安装,一般就下一步下一步了. 服务端下载:https ...