zoukankan      html  css  js  c++  java
  • HDU1253 胜利大逃亡

    这个问题一看,可以说和UVA532 Dungeon Master完全相同,豪情万丈地做了拷贝来程序修改,一提交结果是“Time Limit Exceeded”,满脸困惑。多方调查研究后,终于懂得了程序简洁才是硬道理。也许因为测试数据量大,各个方面改进速度的措施都用了之后,总算是AC了。胜利大逃亡,逃出来了!

    问题链接HDU1253 胜利大逃亡

    问题简述:三维城堡(迷宫),每个点由0(可以经过)和1(墙)组成。输入测试用例数,输入每个例子的立体长宽高和限定的时间,起点是<1,1,1>,终点是<长,宽l高>,移动方向有上、下、左、右、前和后6个方向。每移动一次耗费1分钟,问能否在限定时间内最快走出。如果能则输出最短时间,不能则输出-1。

    问题分析:一个三维迷宫,典型的BFS问题。在BFS搜索过程中,走过的点就不必再走了,因为这次再走下去不可能比上次的步数少。

    程序说明程序中,增加了边界使得判定条件变得简单;0和1的值对换了一下,判定条件也简单了;限定时间条件也用上了,可以减少展开的节点数量。这个程序需要处处节省时间。


    把没有AC的程序也放在这里,可以看出逻辑上没有多少不同,就是不够简洁,结果是超时。

    AC的C++语言程序如下:

    /* HDU1253 胜利大逃亡 */
    
    #include <iostream>
    #include <queue>
    #include <cstdio>
    #include <cstring>
    
    using namespace std;
    
    const int DIRECTSIZE = 6;
    struct direct {
        int dx;
        int dy;
        int dz;
    } direct[DIRECTSIZE] =
        {{-1, 0, 0}, {1, 0, 0}, {0, -1, 0}, {0, 1, 0}, {0, 0, -1}, {0, 0, 1}};
    
    const int MAXN = 50;
    int cube[MAXN+2][MAXN+2][MAXN+2];
    
    struct node {
        int x, y, z, level;
    };
    
    int L, R, C, limit;
    node start;
    
    int bfs()
    {
        queue<node> q;
    
        start.x = 1;
        start.y = 1;
        start.z = 1;
        start.level = 0;
        q.push(start);
    
        cube[1][1][1] = 0;
    
        while(!q.empty()) {
            node front = q.front();
            q.pop();
    
            for(int i=0; i<DIRECTSIZE; i++) {
                int nextx, nexty, nextz;
    
                nextx = front.x + direct[i].dx;
                nexty = front.y + direct[i].dy;
                nextz = front.z + direct[i].dz;
    
                if(cube[nextx][nexty][nextz]) {
                    if(nextx == L && nexty == R && nextz == C)
                        return front.level + 1;
    
                    if(front.level < limit) {
                        cube[nextx][nexty][nextz] = 0;
    
                        node v;
                        v.x = nextx;
                        v.y = nexty;
                        v.z = nextz;
                        v.level = front.level + 1;
                        q.push(v);
                    }
                }
            }
        }
    
        return -1;
    }
    
    int main()
    {
        int t, ans, i, j, k;
    
        scanf("%d", &t);
        while(t--) {
            scanf("%d%d%d%d", &L, &R, &C, &limit);
    
            memset(cube, 0, sizeof(cube));
    
            for(i=1; i<=L; i++)
                for(j=1; j<=R; j++)
                    for(k=1; k<=C; k++) {
                        scanf("%d", &cube[i][j][k]);
                        cube[i][j][k] = 1 - cube[i][j][k];
                    }
    
            ans = bfs();
    
            printf("%d
    ", ans);
        }
    
        return 0;
    }

    没有AC的C++语言程序(Time Limit Exceeded)如下:

    /* HDU1253 胜利大逃亡 */
    
    #include <iostream>
    #include <queue>
    #include <cstdio>
    
    using namespace std;
    
    const int DIRECTSIZE = 6;
    struct direct {
        int dx;
        int dy;
        int dz;
    } direct[DIRECTSIZE] =
        {{-1, 0, 0}, {1, 0, 0}, {0, -1, 0}, {0, 1, 0}, {0, 0, -1}, {0, 0, 1}};
    
    const int MAXN = 50;
    int cube[MAXN][MAXN][MAXN];
    
    struct node {
        int x, y, z, level;
    };
    
    int L, R, C, limit;
    node start, e2;
    int ans;
    
    void bfs()
    {
        queue<node> q;
    
        ans = -1;
        start.x = 0;
        start.y = 0;
        start.z = 0;
        start.level = 0;
        e2.x = L - 1;
        e2.y = R - 1;
        e2.z = C - 1;
        q.push(start);
    
        cube[0][0][0] = 1;
    
        while(!q.empty()) {
            node front = q.front();
            q.pop();
    
            for(int i=0; i<DIRECTSIZE; i++) {
                int nextx, nexty, nextz;
    
                nextx = front.x + direct[i].dx;
                if(0 > nextx || nextx >= L)
                    continue;
                nexty = front.y + direct[i].dy;
                if(0 > nexty || nexty >= R)
                    continue;
                nextz = front.z + direct[i].dz;
                if(0 > nextz || nextz >= C)
                    continue;
    
                if(nextx == e2.x && nexty == e2.y && nextz == e2.z) {
                    ans = front.level + 1;
                    break;
                } else if(front.level < limit && cube[nextx][nexty][nextz] == 0) {
                    cube[nextx][nexty][nextz] = 1;
    
                    node v;
                    v.x = nextx;
                    v.y = nexty;
                    v.z = nextz;
                    v.level = front.level + 1;
                    q.push(v);
                }
    
                if(ans > 0)
                    break;
            }
        }
    }
    
    int main()
    {
        int t, i, j, k;
    
        scanf("%d", &t);
        while(t--) {
            scanf("%d%d%d%d", &L, &R, &C, &limit);
    
            for(i=0; i<L; i++)
                for(j=0; j<R; j++)
                    for(k=0; k<C; k++)
                        scanf("%d", &cube[i][j][k]);
    
            bfs();
    
            cout << ans << endl;
        }
    
        return 0;
    }



  • 相关阅读:
    Wireshark抓包原理(ARP劫持、MAC泛洪)及数据流追踪和图像抓取(二)
    Wireshark抓包原理(ARP劫持、MAC泛洪)及数据流追踪和图像抓取(二)
    Pythonlog() 函数
    Rabbitmq+sockjs+stomp.js前端的使用
    alter table t_user alter column create_date set default CURRENT_TIMESTAMP; 报错(idea生成的sql)
    eldatepicker日期控件日期少一天
    实际开发中String转换为json串作为入参发生"JSON parse error:Cannot deserialize value of type Date......not a valid解决
    Cannot deserialize value of type `java.util.Date` from String
    ProtoBuf试用与JSON的比较
    TCP粘包/拆包问题
  • 原文地址:https://www.cnblogs.com/tigerisland/p/7564434.html
Copyright © 2011-2022 走看看