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 ...
随机推荐
- Frogger--poj2253
http://poj.org/problem?id=2253 题意:The frog distance (humans also call it minimax distance) between t ...
- CF1044B Intersecting Subtrees 构造+树论
正解:构造 解题报告: 传送门 又是一道交互题!爱了爱了! 这题真的,极妙!非常神仙!就非常非常思维题! 直接说解法了吼 说起来实在是简单鸭 就是先问一个对方的联通块中的一个点在我这儿的编号,记为x ...
- 使用QT设计师-信号和槽signal-slot(第一弹)
自定义信号和槽的步骤: 1.定义信号---signal1 = pyqtSignal() 2.定义槽信号---def setSlot(self): 3.连接信号和槽函数---signal1.connec ...
- LeetCode 第 342 题(Power of Four)
LeetCode 第 342 题(Power of Four) Given an integer (signed 32 bits), write a function to check whether ...
- Ubuntu 下Apache安装和配置
在Ubuntu上安装Apache,有两种方式:1 使用开发包的打包服务,例如使用apt-get命令:2 从源码构建Apache.本文章将详细描述这两种不同的安装方式. 方法一:使用开发包的打包服务—— ...
- [py]django第三方分页器django-pure-pagination实战
第三方分页模块: django-pure-pagination 是基于django的pagination做的一款更好用的分页器 参考 配置django-pure-pagination模块 安装 pip ...
- UMI标签学习【转载】
转自: https://club.1688.com/threadview/50123159.htm 简单介绍一下利用单分子标签(Unique Molecular Identifier,UMI)对残留噪 ...
- PAT Product of Polynomials[一般]
1009 Product of Polynomials (25)(25 分) This time, you are supposed to find A*B where A and B are two ...
- IO—代码—基础及其用例
字节流:文件.图片.歌曲 使用字节流的应用场景:如果是读写的数据都不需要转换成字符的时候,则使用字节流. 字节流处理单元为1个字节, 操作字节和字节数组.不能直接处理Unicode字符 字节流可用于任 ...
- python之路 socket、socket server
一.socket socket的英文原义是“孔”或“插座”.作为BSD UNIX的进程通信机制,取后一种意思.通常也 称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,可 ...