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;
    }



  • 相关阅读:
    Python连接MySQL乱码(中文变问号)
    mysql学习04 pymysql 学习
    mysql学习03
    多态与多态性
    重用父类功能的两种方式
    菱形继承问题
    组合
    继承的应用和派生的概念引出
    类的继承
    类与类型
  • 原文地址:https://www.cnblogs.com/icode-girl/p/4579531.html
Copyright © 2011-2022 走看看