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) 首先确立每个框的位置(题目明确说了每一边都不会被完全覆盖)./*可以通过搜索,搜索到该框的所有四个角*/||如果题目 ...
随机推荐
- 彻底理解tomcat是怎样多线程处理http请求并将代码执行到controller里的的
彻底理解tomcat是怎样多线程处理http请求并将代码执行到controller里的的 1.线程池,thread = threadPool.getThread(),thread.executeHtt ...
- Linux多线程实践(四 )线程的特定数据
在单线程程序中.我们常常要用到"全局变量"以实现多个函数间共享数据, 然而在多线程环境下.因为数据空间是共享的.因此全局变量也为全部线程所共同拥有.但有时应用程序设计中有必要提供线 ...
- Vitual Router in The Cloud
VyOS and ESXi,VyOS Configuration The next step is to configure both VyOS routers. Before we do, we s ...
- POI 导入excel数据自己主动封装成model对象--代码分析
上完代码后,对代码进行基本的分析: 1.主要使用反射api将数数据注入javabean对象 2.代码中的日志信息级别为debug级别 3.获取ExcelImport对象后须要调用init()方法初始化 ...
- node15---cookie session
二.Cookie和Session 2.1 Cookie ● HTTP是无状态协议.简单地说,当你浏览了一个页面,然后转到同一个网站的另一个页面,服务器无法认识到,这是同一个浏览器在访问同一个网站.每一 ...
- zzulioj--1707--丧心病狂的计数(水题)
1707: 丧心病狂的计数 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 237 Solved: 105 SubmitStatusWeb Board ...
- Bringing up the Avnet MicroZed with Vivado
Bringing up the Avnet MicroZed with Vivado I recently received the Adam Taylor Edition of Avnet's Zy ...
- SpringMVC(一) 简单代码编写,注解,重定向与转发
SpringMVC是什么 SpringMVC是目前最好的实现MVC设计模式的框架,是Spring框架的一个分支产品,以SpringIOC容器为基础,并利用容器的特性来简化它的配置.SpringMVC相 ...
- WinForm关于listview的用法介绍
public Form1() { InitializeComponent(); //控件的行为 listView1.Bounds = , ), , ));//相对位置 listView1.View = ...
- Servlet学习(九)——request
request运行流程在Servlet学习(四)——response已介绍,不再赘述 1.通过抓包工具获取Http请求 因为request代表请求,所以我们可以通过该对象分别获得Http请求的请求行, ...