POJ1128 (TopSort)(递归)(回溯)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 5220 | Accepted: 1809 |
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
【分析】找到每个字母显示在屏幕上的部分图形的左上角和右上角,然后将出现在该字母边框上的其他字母入度加一,最后拓扑排序。
#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)(递归)(回溯)的更多相关文章
- 递归回溯 UVa140 Bandwidth宽带
本题题意:寻找一个排列,在此排序中,带宽的长度最小(带宽是指:任意一点v与其距离最远的且与v有边相连的顶点与v的距离的最大值),若有多个,按照字典序输出最小的哪一个. 解题思路: 方法一:由于题目说结 ...
- LeetCode || 递归 / 回溯
呜呜呜 递归好不想写qwq 求“所有情况”这种就递归 17. Letter Combinations of a Phone Number 题意:在九宫格上按数字,输出所有可能的字母组合 Input: ...
- 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 ...
- 40. 组合总和 II + 递归 + 回溯 + 记录路径
40. 组合总和 II LeetCode_40 题目描述 题解分析 此题和 39. 组合总和 + 递归 + 回溯 + 存储路径很像,只不过题目修改了一下. 题解的关键是首先将候选数组进行排序,然后记录 ...
- C语言递归回溯法迷宫求解
本例将随机产生一个10*10的迷宫输出后,在下面输出此迷宫的解法. 解法为从坐标(1,1)处进入,从(8,8,)出去,优先线路为先右后下再上最后为左. 不少人求解此题时运用的栈的相关知识,本例寻找线路 ...
- [LeetCode] Sudoku Solver 解数独,递归,回溯
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- hdoj--1016--Prime Ring Problem(递归回溯)
Prime Ring Problem Time ...
- 剑指offer:矩阵中的路径(递归回溯法DFS类似迷宫)
1. 题目描述 /* 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径. 路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子. 如果一条 ...
- 递归回溯生成和解决数独问题c/c++
数独 程序地址https://github.com/papicheng/blog/tree/master/%E6%95%B0%E7%8B%AC 一.游戏规则介绍: 数独是源自18世纪瑞士的一种数学游戏 ...
- 7, java数据结构和算法: 八皇后问题分析和实现 , 递归回溯
什么是八皇后问题: 指的是,在一个8 * 8的棋盘中, 放置8个棋子, 保证这8个棋子相互之间, 不在同一行,同一列,同一斜线, 共有多少种摆法? 游戏连接: http://www.4399.com/ ...
随机推荐
- 【题解】HNOI2018寻宝游戏
太厉害啦……感觉看到了正解之后整个人都惊呆了一样.真的很强%%% 首先要注意到一个性质.位运算列与列之间是不会相互影响的,那么我们先观察使一列满足条件的操作序列需要满足什么条件.&0时,不论之 ...
- [BJOI2006]狼抓兔子——最小割转对偶图最短路
其实这个题直接Dinic跑最小割可过. (小优化是: 无向图建网络流,一条边不用建成4条,可以正反容量都是边权即可.完全等价 ) [无效]网络流之转换对偶图 一个巧妙的事情是,如果建边合适的话,最小割 ...
- [bzoj1033] [ZJOI2008]杀蚂蚁 Big MoNI
这个模拟就不用说了吧...... 注意事项(救命的):1.不能回原位 2.在可以打到target的塔打target的时候,其他打不到的继续打自己的(这是显然的事情只是当时已惘然) 3.如果游戏在某一秒 ...
- 纯css实现 switch开关
<!-- 直接看代码,利用了css3兄弟选择器 --><!-- html --> <button class="switch"> <inp ...
- CVPR2014 Objectness 源码转换(完整版) VS2012 X64 –>win32
一.版本转换 1.将源码中vs2012 X64版本转换为vs2012 win32版本. 2.源码下载及其相关资料下载http://mmcheng.net/zh/bing/ 3.需要下载源码(Pape ...
- NOIP2016愤怒的小鸟 [状压dp]
愤怒的小鸟 题目描述 Kiana 最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于 (0,0) 处,每次 Kiana 可以用它向第一象限发射一只红色的小鸟, ...
- 使用google api material icons在网页中插入图标
在<head></head>中加入这一句: <link rel='stylesheet' href='http://fonts.googleapis.com/icon?f ...
- POJ 2398 Toy Storage 二分+叉积
Description Mom and dad have a problem: their child, Reza, never puts his toys away when he is finis ...
- 在liberty中通过LTPA设置单点登录
不要忘了下面的设置,参考: https://www-01.ibm.com/support/knowledgecenter/was_beta_liberty/com.ibm.websphere.wlp. ...
- 【BZOJ1475】方格取数 [最小割]
方格取数 Time Limit: 5 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Description 在一个n*n的方格里,每个格子里都有一 ...