Frame Stacking
Time Limit: 1000MS | Memory Limit: 10000K |
Description
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.
........ ........ ........ ........ .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.
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
————————————————————集训7.3的分割线————————————————————
前言:这道题无限WA,苦不堪言。深刻意识到了puts( )这个函数隐藏的危险。不,应该说是字符数组隐藏的危险。
思路:首先是建图。虽然没注意到条件2:每张图片的四条边都看得见,但是建图的方法还是注意到了。维护图片的起始行号、终止行号、起始列号和终止列号。然后这个框框上谁被盖住了,就存在一条有向边。
暴力建图,然后注意到题目要求输出拓扑序的反序,而且按照字典序。技巧:反过来拓扑。把有向边都反过来,每次找到出度为0的点即可。
但是要求的输出所有答案,不能再按照队列的方法。要写一个DFS。具体要按照拓扑过程:
找到“入度”为0的点 -> 存入答案数组 -> 访问标记 -> 删除所有相邻边 -> dfs( ) -> 恢复现场
临界(所有顶点进入答案)则输出得到的解。
P.S.关于答案数组,我一直没有想起来在数组末端添加'\0'。。C语言没学好。。。。。。WA得感人肺腑。永远不要认为字符数组会自己添加结束标志!!!
P.S.字母有26个,不知道有哪些,不知道有哪几个
代码如下:
/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <iostream>
using namespace std;
/****************************************/
const int N = 35, M = 26;
int n, m, k, num;
char mat[N][N], path[M+4];
struct Node
{
bool exist;
int r_st, r_ed, c_st, c_ed;
}word[M];
bool G[M][M], vis[M];
int outdeg[M];
void init()
{
num = 0;
memset(outdeg, 0, sizeof(outdeg));
memset(G, 0, sizeof(G));
memset(word, 0, sizeof(word));
}
void All_Topsort(int cnt)
{
if(cnt == num) {
path[num] = '\0';//这里无限WA
puts(path);
return ;
}
for(int i = 0; i < M; i++) if(word[i].exist && !outdeg[i] && !vis[i]) {
path[cnt] = i+'A';
vis[i] = true;
for(int j = 0; j < M; j++) if(word[j].exist) {
if(G[i][j]) {
outdeg[j]--;
}
}
All_Topsort(cnt+1);
for(int j = 0; j < M; j++) if(word[j].exist) {
if(G[i][j]) {
outdeg[j]++;
}
}
vis[i] = false;
}
}
void add(int cur)
{
if(cur >=0 && cur < 26 && cur != k && !G[k][cur]) {
G[k][cur] = true;
outdeg[cur]++;
}
}
int main()
{
while(scanf("%d%d", &n, &m)!=EOF) {
init();
for(int i = 0; i < n; i++) {
scanf("%s", mat[i]);
for(int j = 0; j < m; j++) if(mat[i][j] >= 'A'&&mat[i][j] <= 'Z') {
int c = mat[i][j] - 'A';
if(!word[c].exist) {
word[c].exist = true;
num++;
word[c].r_st = i; word[c].c_st = j;
}
else {
word[c].r_ed = max(word[c].r_ed, i);
word[c].c_ed = max(word[c].c_ed, j);
word[c].r_st = min(word[c].r_st, i);
word[c].c_st = min(word[c].c_st, j);
}
}
}
for(k = 0; k < M; k++) if(word[k].exist) {
for(int i = word[k].r_st; i <= word[k].r_ed; i++) {
int cur1 = mat[i][word[k].c_st] - 'A';
add(cur1);
int cur2 = mat[i][word[k].c_ed] - 'A';
add(cur2);
}
for(int j = word[k].c_st; j <= word[k].c_ed; j++) {
int cur1 = mat[word[k].r_st][j] - 'A';
add(cur1);
int cur2 = mat[word[k].r_ed][j] - 'A';
add(cur2);
}
}
All_Topsort(0);
}
return 0;
}