码迷,mamicode.com
首页 > 其他好文 > 详细

Frame Stacking ZOJ 1083,poj 1128

时间:2014-07-21 00:04:17      阅读:399      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   strong   

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 4034   Accepted: 1352

Description

Consider the following 5 picture frames placed on an 9 x 8 array. 

........ ........ ........ ........ .CCC....
EEEEEE.. ........ ........ ..BBBB.. .C.C....
E....E.. DDDDDD.. ........ ..B..B.. .C.C....
E....E.. D....D.. ........ ..B..B.. .CCC....
E....E.. D....D.. ....AAAA ..B..B.. ........
E....E.. D....D.. ....A..A ..BBBB.. ........
E....E.. DDDDDD.. ....A..A ........ ........
E....E.. ........ ....AAAA ........ ........
EEEEEE.. ........ ........ ........ ........
1 2 3 4 5

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....
ECBCBB..
DCBCDB..
DCCC.B..
D.B.ABAA
D.BBBB.A
DDDDAD.A
E...AAAA
EEEEEE..



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
8
.CCC....
ECBCBB..
DCBCDB..
DCCC.B..
D.B.ABAA
D.BBBB.A
DDDDAD.A
E...AAAA
EEEEEE..

Sample Output

EDABC

Source

[Submit]   [Go Back]   [Status]   [Discuss]

解题思路:由于每条边都能看见部分,那么我们就可以根据所给图得出每张图片的具体位置,然后根据拓扑+DFS得出所有结果,最后再按字典序排下序即可输出答案。

解题代码:

 

bubuko.com,布布扣
  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <algorithm>
  4 #include <string>
  5 #include <iostream>
  6 using namespace std;
  7 const int max_n = 27;
  8 struct data{
  9     int max_x, min_x;
 10     int max_y, min_y;
 11 }data[max_n];
 12 
 13 struct Node{
 14     int cover;
 15     Node *next;
 16 };
 17 Node *Link[max_n], *tm_node;
 18 char map[33][33], ch;
 19 bool vis[max_n];
 20 int cnt[max_n];
 21 string ans[30];
 22 int pos;
 23 int n, m, num;
 24 
 25 int Max(int a, int b){
 26     return a > b ? a : b;
 27 }
 28 
 29 int Min(int a, int b){
 30     return a > b ? b : a;
 31 }
 32 
 33 void find(){
 34     int i, j, k;
 35     for (i = 0; i < max_n; i ++){
 36         if(vis[i]){
 37             for(j = data[i].min_x; j <= data[i].max_x; j ++){
 38                 if(j == data[i].min_x || j == data[i].max_x){
 39                     for(k = data[i].min_y; k <= data[i].max_y; k ++){
 40                         ch = map[j][k] - A;
 41                         if(ch != i){
 42                             tm_node = new Node;
 43                             tm_node ->cover = i;
 44                             tm_node ->next = NULL;
 45                             cnt[i] ++;
 46                             if(Link[ch] == NULL)
 47                                 Link[ch] = tm_node;
 48                             else{
 49                                 tm_node ->next = Link[ch];
 50                                 Link[ch] = tm_node;
 51                             }
 52                         }
 53                     }
 54                 }
 55                 else{
 56                     k = data[i].min_y;
 57                     ch = map[j][k] - A;
 58                     if(ch != i){
 59                         tm_node = new Node;
 60                         tm_node ->cover = i;
 61                         tm_node ->next = NULL;
 62                         cnt[i] ++;
 63                         if(Link[ch] == NULL)
 64                             Link[ch] = tm_node;
 65                         else{
 66                             tm_node ->next = Link[ch];
 67                             Link[ch] = tm_node;
 68                         }
 69                     }
 70                     k = data[i].max_y;
 71                     ch = map[j][k] - A;
 72                     if(ch != i){
 73                         tm_node = new Node;
 74                         tm_node ->cover = i;
 75                         tm_node ->next = NULL;
 76                         cnt[i] ++;
 77                         if(Link[ch] == NULL)
 78                             Link[ch] = tm_node;
 79                         else{
 80                             tm_node ->next = Link[ch];
 81                             Link[ch] = tm_node;
 82                         }
 83                     }
 84                 }
 85             }
 86         }
 87     }
 88 }
 89 
 90 void DFS(int deep, string tm_ans){
 91     if(deep == num){
 92         ans[pos] = "";
 93         for (int j = tm_ans.length() -1; j >= 0 ; j --)
 94             ans[pos] += tm_ans[j];
 95         pos ++;
 96         return;
 97     }
 98     for(int i = 0; i < max_n; i ++){
 99         if(vis[i] && cnt[i] == 0){
100             for(tm_node = Link[i]; tm_node != NULL; tm_node = tm_node ->next)
101                 cnt[tm_node ->cover] --;
102             cnt[i] = -1;
103             ch = i + A;
104             DFS(deep +1, tm_ans + ch);
105             cnt[i] = 0;
106             for(tm_node = Link[i]; tm_node != NULL; tm_node = tm_node ->next)
107                 cnt[tm_node ->cover] ++;
108         }
109     }
110 }
111 
112 int main(){
113     int i, j;
114     while(~scanf("%d%d", &n, &m)){
115         num = 0;
116         memset(vis, 0, sizeof(vis));
117         memset(data, -1, sizeof(data));
118         memset(cnt, 0, sizeof(cnt));
119         memset(Link, 0, sizeof(Link));
120         for(i = 0; i < n; i ++)
121             scanf("%s", map[i]);
122         for(i = 0; i < n; i ++){
123             for(j = 0; j < m; j ++){
124                 char ch = map[i][j];
125                 if(ch == .)
126                     continue;
127                 ch -= A;
128                 if(vis[ch] == 0)
129                     num ++;
130                 vis[ch] = 1;
131                 int tm = data[ch].max_x;
132                 data[ch].max_x = (tm == -1 ? i : Max(tm, i));
133                 tm = data[ch].min_x;
134                 data[ch].min_x = (tm == -1 ? i : Min(tm, i));
135                 tm = data[ch].max_y;
136                 data[ch].max_y = (tm == -1 ? j : Max(tm, j));
137                 tm = data[ch].min_y;
138                 data[ch].min_y = (tm == -1 ? j : Min(tm, j));
139             }
140         }
141         pos = 0;
142         find();
143         DFS(0, "");
144         sort(ans, ans +pos);
145         for (i = 0; i < pos; i ++)
146             cout << ans[i] << endl;
147     }
148     return 0;
149 }
View Code

 

 

 

Frame Stacking ZOJ 1083,poj 1128,布布扣,bubuko.com

Frame Stacking ZOJ 1083,poj 1128

标签:des   style   blog   http   color   strong   

原文地址:http://www.cnblogs.com/shengshouzhaixing/p/3857358.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!