Consider the following five picture frames shown 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 all five picture frames 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 frame, it hides that part of the frame below. Viewing the stack of five frames we see the following.
.CCC... ECBCBB.. DCBCDB.. DCCC.B.. D.B.ABAA D.BBBB.A DDDDAD.A E...AAAA EEEEEE..
Given a picture like this, determine the order of the frames stacked from bottom to top.
Here are the rules for this challenge:
- The width of the frame is always exactly 1 character and the sides are never shorter than 3 characters.
- It is possible to see at least one part of each of the four sides of a frame. A corner is part of two sides.
- The frames will be lettered with capital letters, and no two frames will be assigned the same letter.
PROGRAM NAME: frameup
INPUT FORMAT
Line 1: | Two space-separated integers: the height H (3 <= H <=30) and the width W (3 <= W <= 30). |
Line 2..H+1: | H lines, each with a string W characters wide. |
SAMPLE INPUT (file frameup.in)
9 8 .CCC.... ECBCBB.. DCBCDB.. DCCC.B.. D.B.ABAA D.BBBB.A DDDDAD.A E...AAAA EEEEEE..
OUTPUT FORMAT
Print 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 -- on successive lines. There will always be at least one legal ordering.
SAMPLE OUTPUT (file frameup.out)
EDABC
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define Max(a,b) a>b?a:b
#define Min(a,b) a<b?a:b
FILE *fin,*fout;
int Height,Width;
char Array[32][32];
char Word[27];
int WordC=0;
int Flag[27];
char TmpAns[27];
int AnsCount=0;
char Ans[50000][27];
int comp1(const void *a,const void *b)
{
return strcmp((char *)a,(char *)b);
}
int comp2(const void *a,const void *b)
{
return *(char*)a-*(char*)b;
}
int IsNext(int X,int *Pos1,int *Pos2)
{
int i;
for(i=Pos1[0];i<=Pos2[0];i++){
if(Word[X]!=Array[i][Pos1[1]]&&Array[i][Pos1[1]]!='*')
return 0;
if(Word[X]!=Array[i][Pos2[1]]&&Array[i][Pos2[1]]!='*')
return 0;
}
for(i=Pos1[1];i<=Pos2[1];i++){
if(Word[X]!=Array[Pos1[0]][i]&&Array[Pos1[0]][i]!='*')
return 0;
if(Word[X]!=Array[Pos2[0]][i]&&Array[Pos2[0]][i]!='*')
return 0;
}
return 1;
}
void DFS(int Depth)
{
int Pos1[2],Pos2[2];
int i,j,k;
char Temp[32][32];
if(Depth==WordC){
for(i=Depth-1;i>=0;i--)
Ans[AnsCount][Depth-i-1]=TmpAns[i];
Ans[AnsCount][Depth-i-1]='\0';
AnsCount++;
return;
}
for(i=1;i<=Height;i++)
for(j=1;j<=Width;j++)
Temp[i][j]=Array[i][j];
for(i=0;i<WordC;i++){
if(!Flag[i]){
Pos1[0]=Pos1[1]=33;
Pos2[0]=Pos2[1]=0;
for(j=1;j<=Height;j++)
for(k=1;k<=Width;k++){
if(Array[j][k]==Word[i]){
Pos1[0]=Min(Pos1[0],j);
Pos1[1]=Min(Pos1[1],k);
Pos2[0]=Max(Pos2[0],j);
Pos2[1]=Max(Pos2[1],k);
}
}
if(IsNext(i,Pos1,Pos2)){
TmpAns[Depth]=Word[i];
for(j=Pos1[0];j<=Pos2[0];j++)
Array[j][Pos1[1]]=Array[j][Pos2[1]]='*';
for(j=Pos1[1];j<=Pos2[1];j++)
Array[Pos1[0]][j]=Array[Pos2[0]][j]='*';
Flag[i]=1;
DFS(Depth+1);
Flag[i]=0;
for(j=1;j<=Height;j++)
for(k=1;k<=Width;k++)
Array[j][k]=Temp[j][k];
}
}
}
}
int main()
{
int i,j;
fin=fopen("frameup.in","r");
fout=fopen("frameup.out","w");
memset(Flag,0,sizeof(Flag));
fscanf(fin,"%d %d\n",&Height,&Width);
for(i=1;i<=Height;i++){
for(j=1;j<=Width;j++){
fscanf(fin,"%c",&Array[i][j]);
if(Array[i][j]!='.'&&Flag[Array[i][j]-'A']!=1){
Word[WordC++]=Array[i][j];
Flag[Array[i][j]-'A']=1;
}
}
fscanf(fin,"\n");
}
memset(Flag,0,sizeof(Flag));
qsort(Word,WordC,sizeof(char),comp2);
DFS(0);
qsort(Ans,AnsCount,sizeof(char)*27,comp1);
for(i=0;i<AnsCount;i++)
fprintf(fout,"%s\n",Ans[i]);
return 0;
}
又是一道矩形覆盖的题目。DFS搜索每个字母,分别找到四个角的点,判断这个矩形在当前情况下时候符合要求。
如果符合要求,那么把这个矩形上的字母设定成一个未知量。
下次判断时把未知量作为需要的字母处理。