Frame Stacking
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5220   Accepted: 1809

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
【分析】找到每个字母显示在屏幕上的部分图形的左上角和右上角,然后将出现在该字母边框上的其他字母入度加一,最后拓扑排序。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
//#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include<functional>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
#define maxn 32
#define maxm 28
char ori[maxn][maxn], ans[maxm];
int m, n, in[maxm], total;
bool map[maxm][maxm];
struct Node {
int x, y;
} lt[maxm]; //lefttop
struct Node2 {
int x, y;
} rb[maxm]; //rightTbutton
void getMap() {
int i, j, t, k, x, y;
memset(map, , sizeof(map));
memset(in, -, sizeof(in));
memset(lt, 0x3f, sizeof(lt));
memset(rb, -, sizeof(rb));
for(i = total = ; i < n; ++i)
for(j = ; j < m; ++j) {
if(ori[i][j] == '.') continue;
t = ori[i][j] - 'A';
if(in[t] == -) {
in[t] = ;
++total;
}
if(i < lt[t].x) lt[t].x = i;
if(i > rb[t].x) rb[t].x = i;
if(lt[t].y > j) lt[t].y = j;
if(rb[t].y < j) rb[t].y = j;
}
for(i = ; i < maxm; ++i) {
if(in[i] == -) continue;
for(x = lt[i].x; x <= rb[i].x; ++x)
for(y = lt[i].y; y <= rb[i].y; ++y) {
if(x > lt[i].x && y > lt[i].y && x < rb[i].x && y < rb[i].y)
continue;
t = ori[x][y] - 'A';
if(t != i && !map[i][t]) {
map[i][t] = true;
++in[t];
}
}
}
}
void DFS(int id) {
if(id == total) {
ans[id] = '\0';
puts(ans);
return;
}
for(int i = ; i < maxm; ++i) {
if(in[i] == ) {
ans[id] = 'A' + i;
in[i] = -;
for(int j = ; j < maxm; ++j)
if(map[i][j]) --in[j];
DFS(id + );
in[i] = ;
for(int j = ; j < maxm; ++j)
if(map[i][j]) ++in[j];
}
}
}
int main() {
int i;
while(scanf("%d%d", &n, &m) == ) {
for(i = ; i < n; ++i)
scanf("%s", ori[i]);
getMap();
DFS();
}
return ;
}

POJ1128 (TopSort)(递归)(回溯)的更多相关文章

  1. 递归回溯 UVa140 Bandwidth宽带

    本题题意:寻找一个排列,在此排序中,带宽的长度最小(带宽是指:任意一点v与其距离最远的且与v有边相连的顶点与v的距离的最大值),若有多个,按照字典序输出最小的哪一个. 解题思路: 方法一:由于题目说结 ...

  2. LeetCode || 递归 / 回溯

    呜呜呜 递归好不想写qwq 求“所有情况”这种就递归 17. Letter Combinations of a Phone Number 题意:在九宫格上按数字,输出所有可能的字母组合 Input: ...

  3. FZU - 2038 -E - Another Postman Problem (思维+递归+回溯)

    Chinese Postman Problem is a very famous hard problem in graph theory. The problem is to find a shor ...

  4. 40. 组合总和 II + 递归 + 回溯 + 记录路径

    40. 组合总和 II LeetCode_40 题目描述 题解分析 此题和 39. 组合总和 + 递归 + 回溯 + 存储路径很像,只不过题目修改了一下. 题解的关键是首先将候选数组进行排序,然后记录 ...

  5. C语言递归回溯法迷宫求解

    本例将随机产生一个10*10的迷宫输出后,在下面输出此迷宫的解法. 解法为从坐标(1,1)处进入,从(8,8,)出去,优先线路为先右后下再上最后为左. 不少人求解此题时运用的栈的相关知识,本例寻找线路 ...

  6. [LeetCode] Sudoku Solver 解数独,递归,回溯

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  7. hdoj--1016--Prime Ring Problem(递归回溯)

    Prime Ring Problem                                                                             Time ...

  8. 剑指offer:矩阵中的路径(递归回溯法DFS类似迷宫)

    1. 题目描述 /* 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径. 路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子. 如果一条 ...

  9. 递归回溯生成和解决数独问题c/c++

    数独 程序地址https://github.com/papicheng/blog/tree/master/%E6%95%B0%E7%8B%AC 一.游戏规则介绍: 数独是源自18世纪瑞士的一种数学游戏 ...

  10. 7, java数据结构和算法: 八皇后问题分析和实现 , 递归回溯

    什么是八皇后问题: 指的是,在一个8 * 8的棋盘中, 放置8个棋子, 保证这8个棋子相互之间, 不在同一行,同一列,同一斜线, 共有多少种摆法? 游戏连接: http://www.4399.com/ ...

随机推荐

  1. BZOJ1787 [Ahoi2008]Meet 紧急集合 【LCA】

    1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec  Memory Limit: 162 MB Submit: 3578  Solved: 1635 [Submi ...

  2. size用法小记

    By francis_hao    Feb 14,2017 列出二进制文件各个段的大小和总大小 概述 选项解释 -A -B --format=compatibility 选择显示的格式, -A = - ...

  3. [hdu 4734]数位dp例题

    通过这个题目更加深入了解到了数位dp在记忆化搜索的过程中就是实现了没有限制条件的n位数的状态复用. #include<bits/stdc++.h> using namespace std; ...

  4. 把java的class文件打成jar包的步骤

    现在我的文件夹的目录在: C:\Users\linsenq\Desktop\cglibjar 我要把位于这个目录下的所有文件夹以及这个文件夹下的.class文件打成jar包 第一步:用win+R 打开 ...

  5. bzoj 5094 [Lydsy1711月赛]硬盘检测 概率dp

    [Lydsy1711月赛]硬盘检测 Time Limit: 1 Sec  Memory Limit: 256 MBSubmit: 273  Solved: 75[Submit][Status][Dis ...

  6. koala 编译scss不支持中文解决方案

    方法一: 在scss文件第一行加上代码:@charset "utf-8"; 方法二: 进入到Koala 安装目录 C:\Koala\rubygems\gems\sass-3.4.9 ...

  7. 知问前端——日历UI(二)

    datapicker外观选项 属性 默认值/类型 说明 disabled false/布尔值 禁用日历 numberOfMonths 1/数值 日历中同时显示的月份个数.默认为1,如果设置3就同时显示 ...

  8. bzoj3223 文艺平衡树 codevs3303 翻转区间

    splay模版题吧 只有区间翻转 至于为什么要把须翻转区间旋到根 因为查找一个区间可以先找出他左端点左边第一个点和右端点x右边第一个点y 然后将x旋到根节点 y旋到x的右儿子 这样x的右边的点就是所有 ...

  9. [bzoj3597][scoi2014]方伯伯运椰子——分数规划,负环

    题解 目标就是 \[Maximize\ \lambda = \frac{X-Y}{k}\] 按照分数规划的一般规律, 构造: \[g(\lambda) = \lambda k + Y - X\] 由于 ...

  10. CentOS 7 单用户模式修改root密码

    1)在启动grub菜单,选择编辑选项启动 2)按键盘e键,来进入编辑界面 3)找到Linux 16的那一行,将ro改为rw init=/sysroot/bin/sh 4)现在按下Control+x,使 ...