一 原题
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
二 分析
三 代码
/*
ID:maxkibb3
LANG:C++
PROB:frameup
*/
#include<iostream>
#include<fstream>
#include<vector>
#include<set>
#include<cstring>
using namespace std;
const int MAX = 35;
int H, W;
string C[MAX];
int Border[26][4]; // u, d, l, r
int Num;
bool vis[26];
vector<int> ans;
set<string> re;
void init() {
ifstream fin;
fin.open("frameup.in");
fin >> H >> W;
for(int i = 0; i < 26; i++) {
Border[i][0] = Border[i][2] = 30;
Border[i][1] = Border[i][3] = -1;
}
for(int i = 0; i < H; i++) {
fin >> C[i];
for(int j = 0; j < W; j++) {
if(C[i][j] == '.') continue;
int idx = C[i][j] - 'A';
if(i < Border[idx][0]) Border[idx][0] = i;
if(i > Border[idx][1]) Border[idx][1] = i;
if(j < Border[idx][2]) Border[idx][2] = j;
if(j > Border[idx][3]) Border[idx][3] = j;
}
}
for(int i = 0; i < 26; i++) {
if(Border[i][1] != -1) Num++;
}
}
bool check(int _idx) {
char c = _idx + 'A';
for(int i = Border[_idx][0]; i <= Border[_idx][1]; i++) {
if(C[i][Border[_idx][2]] != c && C[i][Border[_idx][2]] != '*')
return false;
if(C[i][Border[_idx][3]] != c && C[i][Border[_idx][3]] != '*')
return false;
}
for(int i = Border[_idx][2]; i <= Border[_idx][3]; i++) {
if(C[Border[_idx][0]][i] != c && C[Border[_idx][0]][i] != '*')
return false;
if(C[Border[_idx][1]][i] != c && C[Border[_idx][1]][i] != '*')
return false;
}
return true;
}
void dfs(int _depth) {
if(_depth == Num) {
string s = "";
for(int i = ans.size() - 1; i >= 0; i--)
s.append(1, (char)'A' + ans[i]);
re.insert(s);
return;
}
char Cc[MAX][MAX];
for(int i = 0; i < 26; i++) {
if(Border[i][1] == -1) continue;
if(vis[i]) continue;
if(check(i)) {
for(int j = Border[i][0]; j <= Border[i][1]; j++) {
Cc[j][Border[i][2]] = C[j][Border[i][2]];
Cc[j][Border[i][3]] = C[j][Border[i][3]];
C[j][Border[i][2]] = '*';
C[j][Border[i][3]] = '*';
}
for(int j = Border[i][2] + 1; j < Border[i][3]; j++) {
Cc[Border[i][0]][j] = C[Border[i][0]][j];
Cc[Border[i][1]][j] = C[Border[i][1]][j];
C[Border[i][0]][j] = '*';
C[Border[i][1]][j] = '*';
}
ans.push_back(i);
vis[i] = true;
dfs(_depth + 1);
vis[i] = false;
ans.pop_back();
for(int j = Border[i][0]; j <= Border[i][1]; j++) {
C[j][Border[i][2]] = Cc[j][Border[i][2]];
C[j][Border[i][3]] = Cc[j][Border[i][3]];
}
for(int j = Border[i][2] + 1; j < Border[i][3]; j++) {
C[Border[i][0]][j] = Cc[Border[i][0]][j];
C[Border[i][1]][j] = Cc[Border[i][1]][j];
}
}
}
}
void solve() {
ofstream fout;
fout.open("frameup.out");
dfs(0);
for(set<string>::iterator it = re.begin(); it != re.end(); it++) {
fout << *it << endl;
}
}
int main() {
init();
solve();
return 0;
}