zoukankan      html  css  js  c++  java
  • POJ 2251 bfs

    DESCRIPTION:
    给你一个三维的迷宫。问你是否能从起点走到终点。如果能,输出最小步数。对我来说难得就是我没有想到怎么把他给你的三维图转换成map。恩。、好像解题报告上说。只要是这种的最短路都要用bfs。用dfs回很难。不太懂耶。>_<...

    然后就是普通的bfs了。然后忘了三个输入全为0的时候结束程序。然后WA了一会。。然后就没有然后了。233333333333

    附代码:

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #include<iostream>
    using namespace std;

    int l, r, c;
    int ans;
    bool used[40][40][40];
    bool map[40][40][40];

    struct Node
    {
        int l, r, c;
        int step;
        Node()
        {
            step = 0;
        }
    }node[30000], now, temp, st, ed;

    int move[6][3] = {0, 0, 1,  0, 0, -1,  0, 1, 0,  0, -1, 0,  1, 0, 0, -1, 0, 0};

    bool check(int l, int r, int c)
    {
        if (l >= 0 && l < 30 && r >= 0 && r < 30 && c >= 0 && c < 30 && !used[l][r][c] && map[l][r][c])
            return true;
        return false;
    }

    bool bfs(int i, int j, int k)
    {
         int top = 0;
         int tail = 0;
         node[tail++] = st;
         used[st.l][st.r][st.c] = 1;
         while(top < tail)
         {
             now = node[top++];
             if (now.l == ed.l && now.r == ed.r && now.c == ed.c)
             {
                 ans = now.step;
                 return true;
             }
             for (int i=0; i<6; ++i)
             {
                 temp.l = now.l + move[i][0];
                 temp.r = now.r + move[i][1];
                 temp.c = now.c + move[i][2];
                 if (check(temp.l, temp.r, temp.c))
                 {
                     temp.step = now.step + 1;
                     node[tail++] = temp;
                     used[temp.l][temp.r][temp.c] = true;
                 }
             }
         }
         return false;
    }

    int main()
    {
        char t;
        while(cin >> l)
        {
            cin >> r >> c;
            if (l == 0 && r == 0 && c == 0)
                break;
            memset(used, 0, sizeof(used));
            memset(map, 0, sizeof(map));
            for (int ll=0; ll<l; ++ll)
            {
                for (int rr=0; rr<r; ++rr)
                {
                    for (int cc=0; cc<c; ++cc)
                    {
                        cin >> t;
                       if (t == 'S')
                       {
                         map[ll][rr][cc] = true;
                         st.l = ll;
                         st.r = rr;
                         st.c = cc;
                         st.step = 0;
                       }
                       if (t == '.')
                        map[ll][rr][cc] = true;
                       if (t == 'E')
                       {
                           map[ll][rr][cc] = true;
                           ed.l = ll;
                           ed.r = rr;
                           ed.c = cc;
                       }
                    }
                }
            }
            if (bfs(st.l, st.r, st.c))
                cout << "Escaped in " << ans << " minute(s)." << endl;
            else cout << "Trapped! ";
        }
        return 0;
    }



  • 相关阅读:
    028 Android 旋转动画+病毒查杀效果+自定义样式的ProgressBar
    027 Android 可扩展的listview:ExpandableListView的使用案例
    026 Android 带不同类型条目的listview(纯文本类型的条目,图片+文字类型的条目)+读取内存空间、手机进程信息+常驻悬浮框
    025 Android 带进度条的对话框(ProgressDialog)
    024 Android 自定义样式对话框(AlertDialog)
    023 Android 自定义Toast控件
    Cordova-安装Cordova过程详细解
    Symfony3 查询搜索功能DQL语句like查询
    迭代式开发使用方法总结
    require.js vs browserify
  • 原文地址:https://www.cnblogs.com/icode-girl/p/4579531.html
Copyright © 2011-2022 走看看