题目链接:http://poj.org/problem?id=1128点击打开链接
Frame Stacking
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 5646 | Accepted: 1973 |
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
题目的大概意思是有一堆由相同大写字母组成的方框 然后给你从上往下看的图 让你求出所有的可能的从下到上的序列
很明显 从上往下看的时候 如果某框是完整的 那么说明他的入度为0
其他的只需要遍历框就可以记录每个框上面还有几个不同的框 相应的入度为多少
但是这题是从下到上 因此对于一般的拓扑进行反向操作。。
并且因为要求字典序因此从A开始寻找
如此一来便有所有序列
代码有点长
#include <stdio.h>
#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <limits.h>
using namespace std;
char mmap[33][33];
int n,m;
struct xjy
{
int flag;
int x1;
int x2;
int y1;
int y2;
};
xjy re[30];
set<int >s[30];
int rudu[30];
set<int >::iterator it;
int cnt;
int flag[30];
void dfs(string str,int num)
{
if(num==cnt)
{
cout << str << endl;
return ;
}
for(int i=0;i<n;i++)
{
if(rudu[i]==0&&flag[i]==0)
{
flag[i]=1;
vector<int > mmid;
for(it=s[i].begin();it!=s[i].end();it++)
{
mmid.push_back(*it);
rudu[*it]--;
}
dfs(str+char('A'+i),num+1);
flag[i]=0;
for(int j=0;j<mmid.size();j++)
rudu[mmid[j]]++;
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=0;i<33;i++)
for(int j=0;j<33;j++)
mmap[i][j]='.';
for(int i=0;i<30;i++)
{
rudu[i]=1;
s[i].clear();
flag[i]=0;
}
cnt=0;
for(int i=0;i<30;i++)
{
re[i].flag=0;
re[i].x1=INT_MAX;
re[i].x2=INT_MIN;
re[i].y1=INT_MAX;
re[i].y2=INT_MIN;
}
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
scanf(" %c",&mmap[i][j]);
if(mmap[i][j]!='.')
{
int num=mmap[i][j]-'A';
re[num].flag=1;
rudu[num]=0;
re[num].x1=min(re[num].x1,i);
re[num].x2=max(re[num].x2,i);
re[num].y1=min(re[num].y1,j);
re[num].y2=max(re[num].y2,j);
}
}
for(int i=0;i<n;i++)
{
if(re[i].flag)
{
cnt++;
for(int j=re[i].x1;j<=re[i].x2;j++)
{
if(mmap[j][re[i].y1]!=char('A'+i))
{
if(!s[i].count(mmap[j][re[i].y1]-'A'))
{
rudu[mmap[j][re[i].y1]-'A']++;
s[i].insert(mmap[j][re[i].y1]-'A');
}
}
if(mmap[j][re[i].y2]!=char('A'+i))
{
if(!s[i].count(mmap[j][re[i].y2]-'A'))
{
rudu[mmap[j][re[i].y2]-'A']++;
s[i].insert(mmap[j][re[i].y2]-'A');
}
}
}
for(int j=re[i].y1;j<=re[i].y2;j++)
{
if(mmap[re[i].x1][j]!=char('A'+i))
{
if(!s[i].count(mmap[re[i].x1][j]-'A'))
{
rudu[mmap[re[i].x1][j]-'A']++;
s[i].insert(mmap[re[i].x1][j]-'A');
}
}
if(mmap[re[i].x2][j]!=char('A'+i))
{
if(!s[i].count(mmap[re[i].x2][j]-'A'))
{
rudu[mmap[re[i].x2][j]-'A']++;
s[i].insert(mmap[re[i].x2][j]-'A');
}
}
}
}
}
dfs("",0);
}
}