Frame Stacking ZOJ 1083,poj 1128
Description Consider the following 5 picture frames placed on an 9 x 8 array.
........ ........ ........ ........ .CCC.... 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.... 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 Sample Output EDABC Source |
[Submit] [Go Back] [Status] [Discuss]
解题思路:由于每条边都能看见部分,那么我们就可以根据所给图得出每张图片的具体位置,然后根据拓扑+DFS得出所有结果,最后再按字典序排下序即可输出答案。
解题代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;
const int max_n = ;
struct data{
int max_x, min_x;
int max_y, min_y;
}data[max_n]; struct Node{
int cover;
Node *next;
};
Node *Link[max_n], *tm_node;
char map[][], ch;
bool vis[max_n];
int cnt[max_n];
string ans[];
int pos;
int n, m, num; int Max(int a, int b){
return a > b ? a : b;
} int Min(int a, int b){
return a > b ? b : a;
} void find(){
int i, j, k;
for (i = ; i < max_n; i ++){
if(vis[i]){
for(j = data[i].min_x; j <= data[i].max_x; j ++){
if(j == data[i].min_x || j == data[i].max_x){
for(k = data[i].min_y; k <= data[i].max_y; k ++){
ch = map[j][k] - 'A';
if(ch != i){
tm_node = new Node;
tm_node ->cover = i;
tm_node ->next = NULL;
cnt[i] ++;
if(Link[ch] == NULL)
Link[ch] = tm_node;
else{
tm_node ->next = Link[ch];
Link[ch] = tm_node;
}
}
}
}
else{
k = data[i].min_y;
ch = map[j][k] - 'A';
if(ch != i){
tm_node = new Node;
tm_node ->cover = i;
tm_node ->next = NULL;
cnt[i] ++;
if(Link[ch] == NULL)
Link[ch] = tm_node;
else{
tm_node ->next = Link[ch];
Link[ch] = tm_node;
}
}
k = data[i].max_y;
ch = map[j][k] - 'A';
if(ch != i){
tm_node = new Node;
tm_node ->cover = i;
tm_node ->next = NULL;
cnt[i] ++;
if(Link[ch] == NULL)
Link[ch] = tm_node;
else{
tm_node ->next = Link[ch];
Link[ch] = tm_node;
}
}
}
}
}
}
} void DFS(int deep, string tm_ans){
if(deep == num){
ans[pos] = "";
for (int j = tm_ans.length() -; j >= ; j --)
ans[pos] += tm_ans[j];
pos ++;
return;
}
for(int i = ; i < max_n; i ++){
if(vis[i] && cnt[i] == ){
for(tm_node = Link[i]; tm_node != NULL; tm_node = tm_node ->next)
cnt[tm_node ->cover] --;
cnt[i] = -;
ch = i + 'A';
DFS(deep +, tm_ans + ch);
cnt[i] = ;
for(tm_node = Link[i]; tm_node != NULL; tm_node = tm_node ->next)
cnt[tm_node ->cover] ++;
}
}
} int main(){
int i, j;
while(~scanf("%d%d", &n, &m)){
num = ;
memset(vis, , sizeof(vis));
memset(data, -, sizeof(data));
memset(cnt, , sizeof(cnt));
memset(Link, , sizeof(Link));
for(i = ; i < n; i ++)
scanf("%s", map[i]);
for(i = ; i < n; i ++){
for(j = ; j < m; j ++){
char ch = map[i][j];
if(ch == '.')
continue;
ch -= 'A';
if(vis[ch] == )
num ++;
vis[ch] = ;
int tm = data[ch].max_x;
data[ch].max_x = (tm == - ? i : Max(tm, i));
tm = data[ch].min_x;
data[ch].min_x = (tm == - ? i : Min(tm, i));
tm = data[ch].max_y;
data[ch].max_y = (tm == - ? j : Max(tm, j));
tm = data[ch].min_y;
data[ch].min_y = (tm == - ? j : Min(tm, j));
}
}
pos = ;
find();
DFS(, "");
sort(ans, ans +pos);
for (i = ; i < pos; i ++)
cout << ans[i] << endl;
}
return ;
}
Frame Stacking ZOJ 1083,poj 1128的更多相关文章
- zoj 2358,poj 1775 Sum of Factorials(数学题)
题目poj 题目zoj //我感觉是题目表述不确切,比如他没规定xi能不能重复,比如都用1,那么除了0,都是YES了 //算了,这种题目,百度来的过程,多看看记住就好 //题目意思:判断一个非负整数n ...
- POJ 1128 Frame Stacking(拓扑排序·打印字典序)
题意 给你一些矩形框堆叠后的鸟瞰图 推断这些矩形框的堆叠顺序 每一个矩形框满足每边都至少有一个点可见 输入保证至少有一个解 按字典序输出全部可行解 和上一题有点像 仅仅是这个要打印全部的可行 ...
- HDU - 1083 Courses /POJ - 1469
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1083 http://poj.org/problem?id=1469 题意:给你P个课程,并且给出每个课 ...
- Frame Stacking(拓扑排序)
题目链接:http://acm.tju.edu.cn/toj/showp1076.html1076. Frame Stacking Time Limit: 1.0 Seconds Memory ...
- HDU 1815, POJ 2749 Building roads(2-sat)
HDU 1815, POJ 2749 Building roads pid=1815" target="_blank" style="">题目链 ...
- HDU 1325,POJ 1308 Is It A Tree
HDU认为1>2,3>2不是树,POJ认为是,而Virtual Judge上引用的是POJ数据这就是唯一的区别....(因为这个瞎折腾了半天) 此题因为是为了熟悉并查集而刷,其实想了下其实 ...
- HDU 1816, POJ 2723 Get Luffy Out(2-sat)
HDU 1816, POJ 2723 Get Luffy Out pid=1816" target="_blank" style="">题目链接 ...
- POJ 1128 Frame Stacking (拓扑排序)
题目链接 Description Consider the following 5 picture frames placed on an 9 x 8 array. ........ ........ ...
- zoj 1083 Frame Stacking
其实就是一个拓补排序.(动态记录第i个之上的j存不存在,反过来就是第j个之下的i) 首先确立每个框的位置(题目明确说了每一边都不会被完全覆盖)./*可以通过搜索,搜索到该框的所有四个角*/||如果题目 ...
随机推荐
- Qt之窗体拖拽、自适应分辨率、自适应大小
简述 在自定义无边框.标题栏的界面中,需要自己实现最小化.最大化.关闭.窗体背景等功能.最小化.最大化.关闭等按钮设计及功能比较简单,这里就不多做介绍.今天主要介绍一下绘制背景的问题,主要实现自适应屏 ...
- IntelliJ IDEA 初始化项目时No Java SDK Found
IntelliJ IDEA 初始化项目时No Java SDK Found 自己在Project SDK后面的New按钮进行JDK的添加:
- java文件对照工具
今天想比較一下两个java文件.这两个文件是本地的. 就在网上下载了一个对照工具(破解版)认为挺好用的对于不同的地方有高亮显示. 就给大家分享一下.软件名叫:beyond compare 软件下载地址 ...
- ACM这一路
出自自己内心的声音. 大学已经读了一年,自己也老了一岁. 从開始的什么都不懂,到如今的懂了也不想多说什么,说多了也是累.在大学其中唯一还在执着的是ACM.这个也是我唯一能执着的东西,由 ...
- 从零開始学android<TabHost标签组件.二十九.>
TabHost主要特点是能够在一个窗体中显示多组标签栏的内容,在Android系统之中每一个标签栏就称为一个Tab.而包括这多个标签栏的容器就将其称为TabHost.TabHost类的继承结构例如以下 ...
- STM32F407VG (四)时钟配置
1.STM32 F407VG 的starup_stm32f40_41xxx.s的例如以下位置调用 IMPORT SystemInit,之后调用main函数,所以 进入main函数时候就已经自己主动完毕 ...
- spark学习及环境配置
http://dblab.xmu.edu.cn/blog/spark/ 厦大数据库实验室博客 总结.分享.收获 实验室主页 首页 大数据 数据库 数据挖掘 其他 子雨大数据之Spark入门教程 林子 ...
- bzoj4519: [Cqoi2016]不同的最小割(分治最小割)
4519: [Cqoi2016]不同的最小割 题目:传送门 题解: 同BZOJ 2229 基本一样的题目啊,就最后用set记录一下就ok 代码: #include<cstdio> #inc ...
- git使用(公钥私钥产生--远程库添加公钥--本地库关联远程库-使用)
原文1:http://www.cnblogs.com/wangmingshun/p/5424767.html 原文2(指令):http://blog.csdn.net/xiaohanluo/artic ...
- CentOS7安装EPEL的两种方式
转自:http://www.mamicode.com/info-detail-1671603.html epel是社区强烈打造的免费开源发行软件包版本库. EPEL,即Extra Packages f ...