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/ ...
随机推荐
- MySQL之数据库及表的修改和删除
本文章来自实验楼的操作过程和其中相应地解释.(博客园不知道怎么回事,上传图片总是失败.) 一.对数据库修改 1)删除数据库的命令为:DROP DATABASE 数据名; 二.对表的修改 1)重命名一张 ...
- [HEOI2015]最短不公共子串
四合一的题. 简单粗暴的方法: 子串匹配——SAM 子序列匹配——序列自动机 关于序列自动机:序列自动机—— [FJOI2016]所有公共子序列问题 (其实这个玩意没有什么,n+1个点,每个点的字符集 ...
- Dubbo入门介绍---搭建一个最简单的Demo框架
Dubbo入门---搭建一个最简单的Demo框架 置顶 2017年04月17日 19:10:44 是Guava不是瓜娃 阅读数:320947 标签: dubbozookeeper 更多 个人分类: D ...
- Educational Codeforces Round 55 (Rated for Div. 2):D. Maximum Diameter Graph
D. Maximum Diameter Graph 题目链接:https://codeforces.com/contest/1082/problem/D 题意: 给出n个点的最大入度数,要求添加边构成 ...
- hive Illegal Operation state transition from CLOSED to ERROR的处理
异常堆栈如下: 2015-11-24 16:49:11,495 ERROR org.apache.hive.service.cli.operation.Operation: Error running ...
- C#中的弱引用(WeakReference)
我们平常用的都是对象的强引用,如果有强引用存在,GC是不会回收对象的.我们能不能同时保持对对象的引用,而又可以让GC需要的时候回收这个对象呢?.NET中提供了WeakReference来实现.弱引用可 ...
- AngularJs学习——实现数据绑定的三种方式
三种方式: 方式一:<h5>{{msg}}</h5> 此方式在页面刷新的时候会闪现{{}} 方式二:<h5 ng-bind="msg">< ...
- JS模块化工具requirejs教程01
转自:http://www.runoob.com/w3cnote/requirejs-tutorial-1.html 随着网站功能逐渐丰富,网页中的js也变得越来越复杂和臃肿,原有通过script标签 ...
- 转:RBAC权限控制
名词解释: RBAC:Role-Based Access Control,基于角色的访问控制 关键词: RBAC,Java Shiro,Spring Security, 一. RBAC 要解决 ...
- DOM读取和修改节点对象属性
一.获取和修改元素间的内容(3种) 1.innerHTML 获得/设置元素开始标签和结束标签之间的html原文 固定套路:1.删除父元素下所有子元素:parent.innerHTML="&q ...