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 ...
随机推荐
- A Simple Chess---hdu5794(容斥+Lucas)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5794 题意:给你一个n*m的网格,问从(1, 1)走到(n, m)的方案数是多少,其中有r ...
- Best Cow Line---poj3617(贪心)
题目链接:http://poj.org/problem?id=3617 题意:有n头牛.刚开始有一个序列.现在想要重新排列.每次从原始的序列头部和尾部取出一个取出一个放到新的序列尾部.最后使得得到的新 ...
- Find The Multiple--POJ1426
Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose ...
- 我读过的最好的epoll讲解(转)
原文:http://zhihu.com/question/20122137/answer/14049112 作者:蓝形参来源:知乎 首先我们来定义流的概念,一个流可以是文件,socket,pipe等等 ...
- extjs分页
1.本地分页:设置store的proxy属性为pagingmemoryproxy实例 2.远程分页
- python课件-淘宝-目录.txt
卷 TOSHIBA EXT 的文件夹 PATH 列表卷序列号为 AE86-8E8DF:.│ python课件-淘宝-目录.txt│ ├─01python核心编程阶段-linux基础(│ linux_h ...
- 用setup.py安装第三方包packages
这次要说的是用setup.py 来安装第三方包.步骤如下: 步骤:setup.py 先下载你要安装的包,并解压到磁盘下: 进入到该文件的setup.py 目录下 ,打开cmd,并切换到该目录下: 先执 ...
- 4.keras实现-->生成式深度学习之用变分自编码器VAE生成图像(mnist数据集和名人头像数据集)
变分自编码器(VAE,variatinal autoencoder) VS 生成式对抗网络(GAN,generative adversarial network) 两者不仅适用于图像,还可以 ...
- mysql数据库给别人访问权限
注:本操作是在WIN命令提示符下,phpMyAdmin同样适用. 用户:phplamp 用户数据库:phplampDB 1.新建用户. //登录MYSQL @>mysql -u root -p ...
- 转载人家写的CURSOR
转自:http://blog.csdn.net/rdarda/article/details/7881648 1.游标的作用及属性 游标的作用就是用于对查询数据库所返回的记录进行遍历,以便进行相应的操 ...