zoukankan      html  css  js  c++  java
  • POJ1128 (TopSort)(递归)(回溯)

    Frame Stacking
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 5220   Accepted: 1809

    Description

    Consider the following 5 picture frames placed 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 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.

    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
    【分析】找到每个字母显示在屏幕上的部分图形的左上角和右上角,然后将出现在该字母边框上的其他字母入度加一,最后拓扑排序。
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    //#include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #include<functional>
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define pi acos(-1.0)
    using namespace std;
    typedef long long ll;
    #define maxn 32
    #define maxm 28
    char ori[maxn][maxn], ans[maxm];
    int m, n, in[maxm], total;
    bool map[maxm][maxm];
    struct Node {
        int x, y;
    } lt[maxm]; //lefttop
    struct Node2 {
        int x, y;
    } rb[maxm]; //rightTbutton
    void getMap() {
        int i, j, t, k, x, y;
        memset(map, 0, sizeof(map));
        memset(in, -1, sizeof(in));
        memset(lt, 0x3f, sizeof(lt));
        memset(rb, -1, sizeof(rb));
        for(i = total = 0; i < n; ++i)
            for(j = 0; j < m; ++j) {
                if(ori[i][j] == '.') continue;
                t = ori[i][j] - 'A';
                if(in[t] == -1) {
                    in[t] = 0;
                    ++total;
                }
                if(i < lt[t].x) lt[t].x = i;
                if(i > rb[t].x) rb[t].x = i;
                if(lt[t].y > j) lt[t].y = j;
                if(rb[t].y < j) rb[t].y = j;
            }
        for(i = 0; i < maxm; ++i) {
            if(in[i] == -1) continue;
            for(x = lt[i].x; x <= rb[i].x; ++x)
                for(y = lt[i].y; y <= rb[i].y; ++y) {
                    if(x > lt[i].x && y > lt[i].y && x < rb[i].x && y < rb[i].y)
                        continue;
                    t = ori[x][y] - 'A';
                    if(t != i && !map[i][t]) {
                        map[i][t] = true;
                        ++in[t];
                    }
                }
        }
    }
    void DFS(int id) {
        if(id == total) {
            ans[id] = '';
            puts(ans);
            return;
        }
        for(int i = 0; i < maxm; ++i) {
            if(in[i] == 0) {
                ans[id] = 'A' + i;
                in[i] = -1;
                for(int j = 0; j < maxm; ++j)
                    if(map[i][j]) --in[j];
                DFS(id + 1);
                in[i] = 0;
                for(int j = 0; j < maxm; ++j)
                    if(map[i][j]) ++in[j];
            }
        }
    }
    int main() {
        int i;
        while(scanf("%d%d", &n, &m) == 2) {
            for(i = 0; i < n; ++i)
                scanf("%s", ori[i]);
            getMap();
            DFS(0);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    ES6之Promise封装ajax()
    ECMAScript 6 入门之 展开运算符(...)
    函数篇:Callback----回调函数
    Vue-cli组件中写一个节流函数
    使用vant中的地址编辑组件
    js中substr()、substring()、chatAt()的区别
    npm使用国内淘宝镜像的方法
    js数组
    ECMAScript版本
    匿名函数的调用
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/5726485.html
Copyright © 2011-2022 走看看