Frame Stacking(拓扑排序)
题目链接:http://acm.tju.edu.cn/toj/showp1076.html1076. Frame Stacking
Time Limit: 1.0 Seconds Memory Limit: 65536K
Total Runs: 145 Accepted Runs: 54
Consider the following 5 picture frames placed on an 9 x 8 array.
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.
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 DATA
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.
Example input:
9
8
.CCC....
ECBCBB..
DCBCDB..
DCCC.B..
D.B.ABAA
D.BBBB.A
DDDDAD.A
E...AAAA
EEEEEE..
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 DATA
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).
Example Output:
EDABC
题目大意:给定一个摞起来的照片图片,输出照片排放的位置,如果有多种放法的话按字典序从小到大输出所有的解
题解,这是一个很好的拓扑排序的题,题目中说了每个相片的每条边肯定会漏出至少一个字母,所以很容易确定每个相片的位置,及找到这个相片的横坐标和纵坐标的最左端和最右端,最上端,最下端的值
在处理的时候不要忘记对'.'的处理,还有从A~Z不是每个字母都会出现。
建图的时候因为这个图有很多的重边,而且边数不多的时候最好用矩阵存储,在拓扑排序的时候因为是要输出所有的解,所以使用dfs最好。
下面代码中有详细的注释
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
#define N 110
#define INF 0x1fffffff struct Node{
int x1,y1,x2,y2;//x1,y1是最下和最左的点
bool flag;//标记一个字母是否出现过
void init(){//初始化所有点的时候找最大的时候初始化成最小,找最小的时候初始化成最大,才可以不断更新
flag = ;
x1 = y1 = INF;
x2 = y2 = -INF;
}
}node[];
int n , m;
char mp[N][N];//保存一张图
int g[][];//建立关系图
int in[];//入度
int vis[];//是否访问过
void add(int u , int v)
{
g[u][v]=;
}
int total;
void dfs(int cnt , string s)
{
if(cnt==total) printf("%s\n",s.c_str());//dfs当已经把所有的字母顺序都访问过了后就不再进行搜索了
for(int i= ;i < ;i++)
{
if(node[i].flag == &&!vis[i]&&in[i]==)//注意考虑这个字母没有出现的第一个条件
{
vis[i]=;
for(int j = ; j < ; j++)
if(g[i][j]) in[j]--;
char ch = i+'A';
dfs(cnt+,s+ch);//因为找所有解所以后面要还原之前操作
for(int j = ; j < ;j++)
if(g[i][j]) in[j]++;
vis[i]=;
}
}
} void tuop()//拓扑排序
{
total = ;
for(int i = ;i < ; i++)
vis[i] = ,in[i] = ;
for(int i = ; i < ;i++)
for(int j = ;j < ;j++)
if(g[i][j]) in[j]++;
for(int i = ;i < ;i++)
total +=node[i].flag;
dfs(,"");
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i = ;i < n ; i++)
scanf("%s",mp[i]);//如果挨个读入字符两重循环scanf会读入末尾的空行
for(int i = ; i < ; i++)
node[i].init();
for(int i = ;i < n ;i++)
{
for(int j = ; j < m ;j++)
{
if(mp[i][j]=='.') continue;
int id = mp[i][j]-'A';
node[id].flag=;
node[id].x1 = min(node[id].x1,i);
node[id].y1 = min(node[id].y1,j);
node[id].x2 = max(node[id].x2,i);
node[id].y2 = max(node[id].y2,j);
}
}//找到每个字母对应的照片的位置
memset(g,,sizeof(g));
for(int i = ;i < ;i++)
{
if(node[i].flag == ) continue;
int y1 = node[i].y1 , y2 = node[i].y2,x;
for(x = node[i].x1;x<=node[i].x2 ;x++)
{
if(mp[x][y1]!=i+'A') add(i,mp[x][y1]-'A');
if(mp[x][y2]!=i+'A') add(i,mp[x][y2]-'A');
}
int x1 = node[i].x1 , x2 = node[i].x2,y;
for(y = node[i].y1;y<=node[i].y2 ;y++)
{
if(mp[x1][y]!=i+'A') add(i,mp[x1][y]-'A');
if(mp[x2][y]!=i+'A') add(i,mp[x2][y]-'A');
}
}//扫描每条边并建图,如图这条边上本来应该出现同一个字母的时候出现了其他的就说明了其他的字母在这个字母的照片的上面所以建一条从下面字母到上面字母的有向边
tuop();
}
return ;
}
Frame Stacking(拓扑排序)的更多相关文章
- 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 拓扑排序+暴搜
这道题输出特别坑.... 题目的意思也不太好理解.. 就解释一下输出吧.. 它让你 从下往上输出. 如果有多种情况,按照字典序从小往大输出... 就是这个多种情况是怎么产生的呢. 下面给一组样例. 很 ...
- POJ 1128 Frame Stacking(拓扑排序·打印字典序)
题意 给你一些矩形框堆叠后的鸟瞰图 推断这些矩形框的堆叠顺序 每一个矩形框满足每边都至少有一个点可见 输入保证至少有一个解 按字典序输出全部可行解 和上一题有点像 仅仅是这个要打印全部的可行 ...
- POJ1128 Frame Stacking(拓扑排序+dfs)题解
Description Consider the following 5 picture frames placed on an 9 x 8 array. ........ ........ ... ...
- POJ1128 Frame Stacking(拓扑排序)
题目链接:http://poj.org/problem?id=1128 题意:给你一个平面,里面有些矩形(由字母围成),这些矩形互相有覆盖关系,请从求出最底层的矩形到最上层的矩形的序列,如果存在多种序 ...
- 图论之拓扑排序 poj1128 Frame Stacking
题目网址 http://poj.org/problem?id=1128 思路:遍历找出每一种字母出现的最大和最小的横纵坐标,假如本应出现字母A的地方出现了字母B,那么A一定在字母B之前,这就相当于点A ...
- USACO4.4 Frame Up【拓扑排序】
题意居然还读了好久... 读完题目之后大概就知道拓扑排序了.用拓扑可以求出一些字母之间的关系,谁先,谁后.但是这个关系不是唯一确定的,所以就会产生多种方案(题目还要求按字典序输出所有的方案) 输出方案 ...
- ACM/ICPC 之 拓扑排序+DFS(POJ1128(ZOJ1083)-POJ1270)
两道经典的同类型拓扑排序+DFS问题,第二题较第一题简单,其中的难点在于字典序输出+建立单向无环图,另外理解题意是最难的难点,没有之一... POJ1128(ZOJ1083)-Frame Stacki ...
随机推荐
- 637. Average of Levels in Binary Tree
Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...
- Java 管程解决生产者消费者问题
同样是实验存档.//.. 依然以生产者消费者问题作为背景. 管程(=“资源管理程序”)将资源和对资源的操作封装起来,资源使用者通过接口操作资源就 ok,不用去考虑进程同步的问题. 管程: packag ...
- Xamarin Android Gestures详解
通过Gesture的监听我们将实现一个,手指的快速滑动显示坐标的变化,我们先来看一看效果图: 1.Android中手势交互的执行顺序 1.手指触碰屏幕时,触发MotionEvent事件! 2.该事件被 ...
- 2017双11海量数据下EagleEye的使命和挑战
摘要: EagleEye作为阿里集团老牌的链路跟踪系统,其自身业务虽不在交易链路上,但却监控着全集团的链路状态,特别是在中间件的远程调用上,覆盖了集团绝大部分的场景,在问题排查和定位上发挥着巨大的作用 ...
- PHP call_user_func
<?php function my_call_back_function(){ echo "hello world!"; } class MyClass{ static fu ...
- oracle 处理时间和金额大小写的相关函数集合
CREATE OR REPLACE FUNCTION MONEY_TO_CHINESE(MONEY IN VARCHAR2) RETURN VARCHAR2 IS C_MONEY ); M_STRIN ...
- 巧用php中的array_filter()函数去掉多维空值
一直一维array_filter() 函数只能去除一维数组,其实这个函数也能去除多维数组: $arr =[ '0'=>array(), '1'=>'false', '2'=>'tes ...
- 鸟哥的linux私房菜学习-(五)Linux系统的在线求助man page与info page
1.man page man是manual(操作说明)的简写啦!只要下达:『man date』 马上就会有清楚的说明出现在你面前喔!如下所示: 进入man命令的功能后,你可以按下『空格键』往下翻页,可 ...
- SpiderMonkey js引擎的静态编译与使用
原文出处: http://yaolixing.oltag.com/gns-8ABFFE2D-EB1E-44FA-9118-217ED7959536.html 几百KB的跨平台js引擎,是不是您心之所想 ...
- spring boot 之fastJson的使用(二)
昨天说了springboot的简单入门程序.今天进一步深入.今天说一下,fastJson的使用.做过springmvc的都知道fastjson.其实boot自带json可是本人用惯了fastjson, ...