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 ...
随机推荐
- linux下增加磁盘改变指定文件路径分区挂载点和迁移数据
Centos7 系统上原有目录/data 挂载根目录下,空间有点小,我们需要把/data目录挂载到另一个磁盘,同时把数据迁移. 1.查看分区情况 fdisk -l 2.查看路径对应分区情况 df -l ...
- C#中级-通过注册表读取Windows Service程序执行路径
一.前言 假设我们的C#解决方案中有多个程序应用,如:Web应用.控制台程序.WPF程序应用和Windows服务应用. 那么这些非Windows Service应用程序怎么在代码中找到W ...
- 查看内存和cpu
top: 主要参数 d:指定更新的间隔,以秒计算. q:没有任何延迟的更新.如果使用者有超级用户,则top命令将会以最高的优先序执行. c:显示进程完整的路径与名称. S:累积模式,会将己完成或消失的 ...
- > library('ggplot2') Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) : 不存在叫‘colorspace’这个名字的程辑包
> library('ggplot2')Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) : ...
- [编织消息框架][JAVA核心技术]动态代理应用1
前面几篇介绍,终于到了应用阶段啦,我们来做一个RPC来加强学过的知识 做基础核心时先确定解决什么问题,提供什么服务,同将来扩展等 rpc 分两部份,一个是调用者,另一方是服务提供者 调用者只关心那个服 ...
- 关于java字节流的read()方法返回值为int的思考
我们都知道java中io操作分为字节流和字符流,对于字节流,顾名思义是按字节的方式读取数据,所以我们常用字节流来读取二进制流(如图片,音乐 等文件).问题是为什么字节流中定义的read()方法返回值为 ...
- Ubuntu 安装MyEclipse10
Ubuntu 安装MyEclipse10 1.安装JDK 2.下载myeclipse.run 3. 现在假设你的 myeclipse.run 的路径是/home/yourname/myeclipse ...
- margin和padding的区别和用法
margin和padding的区别和用法 什么是margin.padding? marigin:就是外边距.padding:就是内边距.怎么就容易记住两者呢? 马蓉大家都知道吧,给王宝强带帽子的那位, ...
- Linux 文件路径包含特殊字符的处理方式
文件路径包含特殊字符的处理方式 1)只用转义符 \ 2)使用双引号 mv /home/".Sent Items"/ /home/".&XfJT0ZABkK5O9g ...
- socket对象放在一个datagridview的row的tag里面在拿出来 为什么是已释放
socket对象放在一个datagridview的row的tag里面在拿出来 为什么是已释放