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) 首先确立每个框的位置(题目明确说了每一边都不会被完全覆盖)./*可以通过搜索,搜索到该框的所有四个角*/||如果题目 ...
随机推荐
- Sublime Text 3 添加到右键菜单
Sublime Text 3 添加到右键菜单 学习了:http://jingyan.baidu.com/article/cdddd41c99d07653ca00e147.html Windows Re ...
- 阿里云部署Docker(8)----安装和使用redmine
安装redmine对过程进行管理. 须要说明的是:当你在docker images的时候,会说没连接到xxxx的时候,并且会提示用"docker -d".事实上这仅仅是把docke ...
- QStandardItemModel的data线程安全(在插入数据时,临时禁止sizeHint去读model中的data)
版权声明:本文为博主原创文章,欢迎转载,转载请注明出处 https://blog.csdn.net/MatchYang/article/details/52988257 在直接使用QStandardI ...
- 头像文件上传 方法一:from表单 方法二:ajax
方法一:from表单 html 设置form表单,内包含头像预览div,内包含上传文件input 设置iframe用来调用函数传参路径 <!--表单提交成功后不跳转处理页面,而是将处理数据返回给 ...
- Android之通过HttpURLConnection.getResponseCode状态码抛出异常的问题以及解决方法
1.最近,在学习解析json数据的时候遇到一个错误信息,错误信息如下图所示: 发现解析出来的数据为空,错误信息如上图所示,发现程序中的HttpUtils工具类的22行出现了错误和MainActiv ...
- Java文件(io)编程——文件字节流的使用
案例1: 演示FileInputStream类的使用(用FileInputStream的对象把文件读入到内存) 首先要在E盘新建一个文本文件,命名为test.txt,输入若干字符 public cla ...
- javascript满天小星星
- js 关于height()、innerHeight()、outerHeight()函数的区别
- keepalived + nginx 实现高可用
原理 nginx 可以实现负载均衡,但 nginx 自身存在单点故障的问题,这时候最先想到的就是 keepalived,可以解决单点故障的问题 由于没有使用 lvs,所以这里 nginx 之间不存在负 ...
- Union File System
目录 Union File System AUFS Docker是如何使用AUFS的 image layer 和 AUFS (docker版本不同可能会有区别,我的是在/var/lib/docker下 ...