POJ1128 Frame Stacking(拓扑排序+dfs)题解
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
题意:给你一张图,图里有很多矩形,矩形的边框用不同的字母表示,这些矩形重叠在一起,要你给出从上到下一次的顺序
思路:首先一个坑点,该顺序不唯一(如果完全没重叠就用按字典序排),所以不用队列来做拓扑排序了,这里是dfs。记录一个矩形只需要记录对角两个点就够了,然后我们看矩形的边上有没有其他字母,有的话就说明另一个字符在上面,相应邻接表置为1,然后dfs。
代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<queue>
#include<cmath>
#include<string>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<iostream>
#include<algorithm>
#include<sstream>
#define ll long long
const int N=210;
const int INF=1e9;
using namespace std;
struct node{
int x1,y1,x2,y2;
}pos[30];
char mp[N][N];
int n,m,in[N],exist[30],table[30][30];
void frame(){
memset(in,0,sizeof(in));
for(int i=0;i<n;i++){ //确定边框
for(int j=0;j<m;j++){
if(mp[i][j]=='.') continue;
int x=mp[i][j]-'A';
exist[x]=1;
if(i<pos[x].x1) pos[x].x1=i;
if(j<pos[x].y1) pos[x].y1=j;
if(i>pos[x].x2) pos[x].x2=i;
if(j>pos[x].y2) pos[x].y2=j;
}
}
for(int i;i<26;i++){ //重叠计算
if(exist[i]){
for(int j=pos[i].x1;j<=pos[i].x2;j++){
int tmp=mp[j][pos[i].y1]-'A';
if(table[i][tmp]==0 && i!=tmp){
table[i][tmp]=1;
in[tmp]++;
}
tmp=mp[j][pos[i].y2]-'A';
if(table[i][tmp]==0 && i!=tmp){
table[i][tmp]=1;
in[tmp]++;
}
}
for(int j=pos[i].y1;j<=pos[i].y2;j++){
int tmp=mp[pos[i].x1][j]-'A';
if(table[i][tmp]==0 && i!=tmp){
table[i][tmp]=1;
in[tmp]++;
}
tmp=mp[pos[i].x2][j]-'A';
if(table[i][tmp]==0 && i!=tmp){
table[i][tmp]=1;
in[tmp]++;
}
}
}
}
}
int cnt;
char ans[30];
void dfs(int num){
if(num==cnt){
ans[cnt]='\0';
printf("%s\n",ans);
return;
}
for(int i=0;i<26;i++){
if(exist[i] && in[i]==0){
ans[num]='A'+i;
in[i]=-1;
for(int j=0;j<26;j++){
if(table[i][j]){
in[j]--;
}
}
dfs(num+1);
in[i]=0;
for(int j=0;j<26;j++){
if(table[i][j]){
in[j]++;
}
}
}
}
}
void init(){
memset(exist,0,sizeof(exist));
memset(in,0,sizeof(in));
memset(table,0,sizeof(table));
for(int i=0;i<N;i++){
pos[i].x1=100;
pos[i].x2=-1;
pos[i].y1=100;
pos[i].y2=-1;
}
}
int main(){
while(~scanf("%d%d",&n,&m)){
init();
for(int i=0;i<n;i++){
scanf("%s",mp[i]);
}
frame();
cnt=0;
for(int i=0;i<30;i++){
if(exist[i]) cnt++;
}
dfs(0);
}
return 0;
}
POJ1128 Frame Stacking(拓扑排序+dfs)题解的更多相关文章
- POJ 1128 Frame Stacking (拓扑排序)
题目链接 Description Consider the following 5 picture frames placed on an 9 x 8 array. ........ ........ ...
- Frame Stacking 拓扑排序 图论
Description Consider the following 5 picture frames placed on an 9 x 8 array. ........ ........ .... ...
- POJ 1128 Frame Stacking 拓扑排序+暴搜
这道题输出特别坑.... 题目的意思也不太好理解.. 就解释一下输出吧.. 它让你 从下往上输出. 如果有多种情况,按照字典序从小往大输出... 就是这个多种情况是怎么产生的呢. 下面给一组样例. 很 ...
- ACM/ICPC 之 拓扑排序+DFS(POJ1128(ZOJ1083)-POJ1270)
两道经典的同类型拓扑排序+DFS问题,第二题较第一题简单,其中的难点在于字典序输出+建立单向无环图,另外理解题意是最难的难点,没有之一... POJ1128(ZOJ1083)-Frame Stacki ...
- 拓扑排序+DFS(POJ1270)
[日后练手](非解题) 拓扑排序+DFS(POJ1270) #include<stdio.h> #include<iostream> #include<cstdio> ...
- POJ1128 Frame Stacking(拓扑排序)
题目链接:http://poj.org/problem?id=1128 题意:给你一个平面,里面有些矩形(由字母围成),这些矩形互相有覆盖关系,请从求出最底层的矩形到最上层的矩形的序列,如果存在多种序 ...
- 图论之拓扑排序 poj1128 Frame Stacking
题目网址 http://poj.org/problem?id=1128 思路:遍历找出每一种字母出现的最大和最小的横纵坐标,假如本应出现字母A的地方出现了字母B,那么A一定在字母B之前,这就相当于点A ...
- Ordering Tasks(拓扑排序+dfs)
Ordering Tasks John has n tasks to do. Unfortunately, the tasks are not independent and the executio ...
- HDU 5438 拓扑排序+DFS
Ponds Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Sub ...
随机推荐
- LightOj 1104 - Birthday Paradox(生日悖论概率)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1104 题意:一年365天,在有23个人的情况下,这23个人中有两个人生日相同的概率是大 ...
- 洛谷P4436 游戏 [HNOI/AHOI2018]
正解:拓扑排序 解题报告: 传送门! 首先不难想到可以把麻油锁的一段先直接缩成一个点,然后预处理每个点能到达的最左和最右节点,然后就能O(1)地查询辣 所以难点在于预处理 可以想到,对于它给定的关于锁 ...
- 牛客练习赛16D K进制 数论(待理解QAQ)
正解:数论 解题报告: 行吧那就让我一点点推出来趴QAQ
- Shiro的使用
前言 相比有做过企业级开发的童鞋应该都有做过权限安全之类的功能吧,最先开始我采用的是建用户表,角色表,权限表,之后在拦截器中对每一个请求进行拦截,再到数据库中进行查询看当前用户是否有该权限,这样的设计 ...
- 【Python练习】文件引用用户名密码登录系统
一.通过txt文件引入用户名密码 1 #coding=utf-8 from selenium import webdriver #from selenium.common.exceptions imp ...
- InnoSQL/MySQL并行复制的实现与配置
InnoSQL/MySQL并行复制的实现与配置 http://www.innomysql.net/article/6276.html 并行复制之前的解决方案 InnoSQL在5.5.30-v4版本中支 ...
- MySQL中MyISAM与InnoDB区别及选择,mysql添加外键
InnoDB:支持事务处理等不加锁读取支持外键支持行锁不支持FULLTEXT类型的索引不保存表的具体行数,扫描表来计算有多少行DELETE 表时,是一行一行的删除InnoDB 把数据和索引存放在表空间 ...
- Andrew Ng-ML-第八章-正则化
1.过度拟合overfitting 过度拟合,因为有太多的特征+过少的训练数据,学习到的假设可能很适应训练集,但是不能泛化到新的样例.即泛化generalize能力差. 解决办法: 1.手动/使用选择 ...
- 【Cocos2dx 3.3 Lua】滚动字幕
参考资料: http://blog.csdn.net/jackystudio/article/details/12991977 1.原理 通过调用update来更新位置达到 ...
- 修改class文件
http://yucaifu1989.iteye.com/blog/1850500 http://blog.csdn.net/hexin373/article/details/6669813 使用ja ...