Frame Stacking
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 5507 | Accepted: 1924 |
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
对于一个矩形,可以用两点表示,即左上和右下
虽然矩形与矩形之间可能会覆盖,但题目中给出,至少会看到四条边中的一部分,此时就可以刷新横纵坐标的最小值和最大值求出矩形的左上和右下;
矩形表示好了,就要思考另一个问题,如何判断一个矩形在另一个矩形之上?
可以遍历矩形的四条边,如果一个矩形这四条边上有其他字母,那么这个其他字母一定是其他矩形覆盖了这个矩形,
对于这种覆盖问题通常都是用拓扑排序解决,既然要求从下到上的顺序,此时就要正向建边
最后用进行拓扑排序,由于出现多种状态需要按字典序输出,所以用dfs从小到大枚举每一种满足拓扑排序的情况,尾状态输出即可
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<string>
#include<queue>
#include<algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int inf=0x3f3f3f3f;
const int N=35;
struct zuoshang
{
int x,y;
}lu[N];//矩形的左上
struct youxia
{
int x,y;
}rd[N];//矩形的右下
char map[N][N],ans[N];
int n,m,tot,id[N],e[N][N];
void read()
{
mem(id,-1);
mem(lu,0x3f);
mem(rd,-1);
mem(e,0);
tot=0;//标记一共多少种字母
for(int i=0;i<n;i++)
scanf("%s",map[i]);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(map[i][j]=='.')continue;
int t=map[i][j]-'A';
if(id[t]==-1)id[t]=0,tot++;
//刷新矩形的左上和右下
lu[t].x=min(lu[t].x,i);
lu[t].y=min(lu[t].y,j);
rd[t].x=max(rd[t].x,i);
rd[t].y=max(rd[t].y,j);
}
}
}
void build_edge()
{
for(int k=0;k<N;k++)
{
if(id[k]==-1)continue;
for(int x=lu[k].x;x<=rd[k].x;x++)
{
for(int y=lu[k].y;y<=rd[k].y;y++)
{
if(x>lu[k].x&&x<rd[k].x&&y>lu[k].y&&y<rd[k].y)continue;//使只判断边,忽视矩形内部
int t=map[x][y]-'A';
if(t!=k&&!e[k][t])//防止入度重复计算
{
e[k][t]=1;
id[t]++;
}
}
}
}
}
void dfs(int step)
{
if(step==tot)
{
ans[step]='\0';
puts(ans);
return;
}
for(int i=0;i<N;i++)//从0开始找串,保证满足字典序
{
if(id[i]==0)
{
ans[step]=char(i+'A');
id[i]=-1;
for(int j=0;j<N;j++)
{
if(e[i][j])
{
id[j]--;
}
}
dfs(step+1);
id[i]=0;
for(int j=0;j<N;j++)
{
if(e[i][j])
{
id[j]++;
}
}
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)==2)
{
read();
build_edge();
dfs(0);
}
}