Description

Consider the following 5 picture frames placed on an 9 x 8 array. 
........ ........ ........ ........ .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

Each input block contains the height, h (h<=30) on the first line and the width w (w<=30) on the second. A picture of the stacked frames is then given as h strings with w characters each. 
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

Write the solution to the standard output. Give the letters of the frames in the order they were stacked from bottom to top. If there are multiple possibilities for an ordering, list all such possibilities in alphabetical order, each one on a separate line. There will always be at least one legal ordering for each input block. List the output for all blocks in the input sequentially, without any blank lines (not even between blocks).

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)题解的更多相关文章

  1. POJ 1128 Frame Stacking (拓扑排序)

    题目链接 Description Consider the following 5 picture frames placed on an 9 x 8 array. ........ ........ ...

  2. Frame Stacking 拓扑排序 图论

    Description Consider the following 5 picture frames placed on an 9 x 8 array. ........ ........ .... ...

  3. POJ 1128 Frame Stacking 拓扑排序+暴搜

    这道题输出特别坑.... 题目的意思也不太好理解.. 就解释一下输出吧.. 它让你 从下往上输出. 如果有多种情况,按照字典序从小往大输出... 就是这个多种情况是怎么产生的呢. 下面给一组样例. 很 ...

  4. ACM/ICPC 之 拓扑排序+DFS(POJ1128(ZOJ1083)-POJ1270)

    两道经典的同类型拓扑排序+DFS问题,第二题较第一题简单,其中的难点在于字典序输出+建立单向无环图,另外理解题意是最难的难点,没有之一... POJ1128(ZOJ1083)-Frame Stacki ...

  5. 拓扑排序+DFS(POJ1270)

    [日后练手](非解题) 拓扑排序+DFS(POJ1270) #include<stdio.h> #include<iostream> #include<cstdio> ...

  6. POJ1128 Frame Stacking(拓扑排序)

    题目链接:http://poj.org/problem?id=1128 题意:给你一个平面,里面有些矩形(由字母围成),这些矩形互相有覆盖关系,请从求出最底层的矩形到最上层的矩形的序列,如果存在多种序 ...

  7. 图论之拓扑排序 poj1128 Frame Stacking

    题目网址 http://poj.org/problem?id=1128 思路:遍历找出每一种字母出现的最大和最小的横纵坐标,假如本应出现字母A的地方出现了字母B,那么A一定在字母B之前,这就相当于点A ...

  8. Ordering Tasks(拓扑排序+dfs)

    Ordering Tasks John has n tasks to do. Unfortunately, the tasks are not independent and the executio ...

  9. HDU 5438 拓扑排序+DFS

    Ponds Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Sub ...

随机推荐

  1. 两种JS事件流

    1.事件冒泡流(从注册事件元素到外侧容器元素的过程): 2.事件捕获流(与冒泡相反的过程):

  2. javaScript错误(一)Cannot call method 'addEventListener' of null

    Cannot call method 'addEventListener' of null 原因很简单,JavaScript代码中要引用到DOM对象,但是这个DOM对象在JavaScript执行后才会 ...

  3. redis连接池的标准用法:

    from .conf import HOST, PORT, POOL_NAME import redis redis_pool = redis.ConnectionPool(host=HOST, po ...

  4. 梯度下降算法(Gradient Descent)

    近期在搞论文,须要用梯度下降算法求解,所以又一次整理分享在这里. 主要包含梯度介绍.公式求导.学习速率选择.代码实现. 梯度下降的性质: 1.求得的解和选取的初始点有关 2.能够保证找到局部最优解,由 ...

  5. 011-jdk1.8版本新特性三-Date API

    1.7.Date API Java 8 在包java.time下包含了一组全新的时间日期API.新的日期API和开源的Joda-Time库差不多,但又不完全一样,下面的例子展示了这组新API里最重要的 ...

  6. Win10+vs2012+cuda8.0的安装与配置

    安装环境说明:NVDIA GeForce 930M.Intel(R) HD Graphics 520 显卡和cuda需要兼容匹配,我一开始下载的cuda6.5无法安装,所以又重新下了比较新的cuda8 ...

  7. Scala里面如何使用枚举

    枚举通常用来定义已知数量的常量,比如月份,星期,季节等等,用过java的人都知道定义枚举的关键字是enum,在scala里面和java有所不同,来看一个完整的例子定义: object EnumTest ...

  8. Docker 后台进程参数-------更改Docker运行根目录的方法

    参数 介绍 --api-enable-cors=false 远程API调用. -b, --bridge="" 桥接一个系统上的网桥设备到 Docker 容器里,当使用 none 可 ...

  9. Py之pandas:dataframe学习【转载】

    转自:https://www.tutorialspoint.com/python_pandas/python_pandas_dataframe.htm 1.数据框4特性 列是不同类型的数据元素. 每列 ...

  10. openstack 部署笔记--neutron控制节点

    控制节点 配置neutron用户及服务 $ . admin-openrc $ openstack user create --domain default --password-prompt neut ...