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/ ...
随机推荐
- centos7 mysql cluster集群搭建基于docker
1.准备 mn:集群管理服务器用于管理集群的其他节点.我们可以从管理节点创建和配置集群上的新节点.重新启动.删除或备份节点. db2/db3:这是节点间同步和数据复制的过程发生的层. db4/db5: ...
- java程序在centos7里面开机自启动
1.我们先来个简单的start,status,stop程序: [root@localhost ~]# cat /home/tomcat/jarservice.sh #!/bin/bashCU_PID= ...
- idea使用(一)
基本上正式开发的常用工具基本都集成了,而且基本都在你非常容易触到的位置.说说我比较常用的: 1.ant 你懂的 2.maven你也懂的 3.SVN相比之下,IDEA的SVN的提交提供了更多的选项和功能 ...
- 面向对象的tab选项卡实现
利用最基础的面向对象的思想,实现tab选项卡效果: 效果截图:
- oracle11g 使用数据泵导出导入数据
终于搞定了 快写个笔记 记录下. 删除用户的时候提示已经登录了不能删除,这个需要把登录的session结束掉. select username,sid,serial# from v$session w ...
- 【BZOJ2326】【HNOI2011】数学作业 [矩阵乘法][DP]
数学作业 Time Limit: 10 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description Input 输入文件只有一行为用空 ...
- 【BZOJ】1579: [Usaco2009 Feb]Revamping Trails 道路升级
[算法]分层图最短路 [题解] 考虑k层一模一样的图,然后每个夹层都在每条边的位置新加从上一层跨越到下一层的边权为0的边,这样至多选择k条边置为0. 然后考虑方便的写法. SPFA 第一次SPFA计算 ...
- Nexus 5 Change FireFox OS to android
1.Enter Fastboot mode,flash recovery: D:\BaiduYunDownload\recovery>fastboot flash recovery 6.0.4. ...
- 如何加快Eclipse的启动速度?
http://it.taocms.org/12/6457.htm 浅析配置更快的Eclipse方法 What is the Metadata GC Threshold and how do I tun ...
- 嵌入式上 iscsi实现
前言 去年公司设备(haisi3516)上需要提供iscsi的功能,于是花了几天时间探究了下.linux内核(2.6.xx)支持iscsi,只是我发现当时我们设备的内核编译时没有选上,于是重新编译了内 ...