USACO 4.4 Frame Up

Frame Up

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


————————————————————————————————题解
只要不断判断有没有一个完全的矩形,去掉,用通配符代替即可如‘*’
但是我调试了比较长时间,被自己挖的坑坑到了
可是这就是一道简单的暴搜啊……上面是一道网络流题这USACO画风清奇……
  1 /*
  2 ID: ivorysi
  3 LANG: C++
  4 TASK: frameup
  5 */
  6 #include <iostream>
  7 #include <cstdio>
  8 #include <cstring>
  9 #include <algorithm>
 10 #include <queue>
 11 #include <set>
 12 #include <vector>
 13 #include <string.h>
 14 #define siji(i,x,y) for(int i=(x);i<=(y);++i)
 15 #define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
 16 #define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
 17 #define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
 18 #define inf 0x7fffffff
 19 #define ivorysi
 20 #define mo 97797977
 21 #define hash 974711
 22 #define base 47
 23 #define pss pair<string,string>
 24 #define MAXN 30005
 25 #define fi first
 26 #define se second
 27 #define pii pair<int,int>
 28 using namespace std;
 29 struct node {
 30     string as;
 31     char gr[35][35];
 32     int used[30];
 33     node() {
 34         as="";
 35         memset(gr,'\0',sizeof(gr));
 36         memset(used,0,sizeof(used));
 37     }
 38 };
 39 queue<node> q;
 40 int h,w;
 41 int top[30],bottom[30],lef[30],righ[30],view[30];
 42 vector<string> ans;
 43 vector<int> le;
 44 void init() {
 45     scanf("%d%d",&h,&w);
 46     node f;
 47     siji(i,1,h) {
 48         scanf("%s",f.gr[i]+1);
 49     }
 50     q.push(f);
 51     fill(top,top+27,inf);//fill(first,last,val);
 52     fill(lef,lef+27,inf);//这里写错了,应该是lef而不是lef+1
 53     siji(i,1,h) {
 54         siji(j,1,w) {
 55             if(f.gr[i][j]<'A' || f.gr[i][j]>'Z' ) continue;
 56             int k=f.gr[i][j]-'A';
 57             if(!view[k]) {le.push_back(k);view[k]=1;}
 58             
 59             if(i<top[k]) top[k]=i;
 60             if(i>bottom[k]) bottom[k]=i;
 61             if(j<lef[k]) lef[k]=j;
 62             if(j>righ[k]) righ[k]=j;
 63 
 64         }
 65     }
 66     sort(le.begin(),le.end());
 67 }
 68 void bfs() {
 69     init();
 70     int cnt=0;
 71     int wrong=0;
 72     while(!q.empty()) {
 73         node now=q.front();q.pop();
 74         /*printf("-----%d-----\n",++cnt);
 75         for(int i=1;i<=h;++i) {
 76             printf("%s\n",now.gr[i]+1);
 77         }*/
 78         if(now.as.length()==le.size()) {ans.push_back(now.as);continue;}
 79         node t=now;
 80         xiaosiji(i,0,le.size()) {
 81             if(t.used[le[i]]) continue;
 82             siji(j,lef[le[i]],righ[le[i]]) {
 83                 if(now.gr[top[le[i]]][j]=='*' || now.gr[top[le[i]]][j]=='A'+le[i]) {
 84                     t.gr[top[le[i]]][j]='*';
 85                 }
 86                 else {wrong=1;goto fail;}
 87             }
 88             siji(j,lef[le[i]],righ[le[i]]) {
 89                 if(now.gr[bottom[le[i]]][j]=='*' || now.gr[bottom[le[i]]][j]=='A'+le[i]) {
 90                     t.gr[bottom[le[i]]][j]='*';
 91                 }
 92                 else {wrong=2;goto fail;}
 93             }
 94             siji(j,top[le[i]],bottom[le[i]]) {
 95                 if(now.gr[j][lef[le[i]]]=='*' || now.gr[j][lef[le[i]]]=='A'+le[i]) {
 96                     t.gr[j][lef[le[i]]]='*';
 97                 }
 98                 else {wrong=3;goto fail;}
 99             }
100             siji(j,top[le[i]],bottom[le[i]]) {
101                 if(now.gr[j][righ[le[i]]]=='*' || now.gr[j][righ[le[i]]]=='A'+le[i]) {
102                     t.gr[j][righ[le[i]]]='*';
103                 }
104                 else {wrong=4;goto fail;}
105             }
106             t.as.append(1,'A'+le[i]);
107             t.used[le[i]]=1;
108             q.push(t);
109             t=now;continue;
110             fail:
111             //printf("%d %c %d\n",cnt,le[i]+'A',wrong);
112             //printf("%d %d %d %d\n",top[le[i]],bottom[le[i]],lef[le[i]],righ[le[i]]);
113             t=now;
114         }
115     }
116 }
117 void solve() {
118     bfs();
119     xiaosiji(i,0,ans.size()) {
120         reverse(ans[i].begin(),ans[i].end());
121     }
122     sort(ans.begin(),ans.end());
123     xiaosiji(i,0,ans.size()) {
124         cout<<ans[i]<<endl;
125     }
126 } 
127 int main(int argc, char const *argv[])
128 {
129 #ifdef ivorysi
130     freopen("frameup.in","r",stdin);
131     freopen("frameup.out","w",stdout);
132 #else
133     freopen("f1.in","r",stdin);
134 #endif
135     solve();
136     return 0;
137 }

 



posted @ 2017-02-03 19:33  sigongzi  阅读(488)  评论(0编辑  收藏  举报