南极亚拉 2017-08-04 05:48 采纳率: 0%
浏览 1012

POJ 2251 Dungeon Master(搜索)不知道为什么会超内存,求看看

我的思路是每次入队之后,把该位置设置为墙,所以就没有用vis数组。
把边界都设置为墙,所以可以不用考虑边界。
然后正常BFS走一遍。
结果 the memory limited 了

#include
#include
#include
#include
#include

#define MAX_N 32

using namespace std;
char map[MAX_N][MAX_N][MAX_N];
int L, R, C;

struct pos
{
int l;
int r;
int c;
int t;
};

int main()
{

while (cin >> L >> R >> C)
{
    queue<pos>q1;
    pos t;
    if (L == 0 && R == 0 && C == 0)break;
    memset(map, '#', sizeof(map));
    for (int i = 1; i <= L; i++)
    {
        for (int j = 1; j <= R; j++)
        {
            for (int k = 1; k <= C; k++)
            {
                cin >> map[i][j][k];
                if (map[i][j][k] == 'S')
                {
                    t.l = i;
                    t.r = j;
                    t.c = k;
                    t.t = 0;
                    q1.push(t);
                    map[i][j][k] = '#';
                }
            }
        }
    }
    int flag = 0;
    int ans = 0;
    while (!q1.empty())
    {   
        pos temp1 = q1.front();
        if (map[temp1.l][temp1.r][temp1.c] == 'E') { flag = 1; ans = q1.front().t; break; }

        map[temp1.l][temp1.r][temp1.c] = '#';
        q1.pop();
        temp1.t++;
        pos temp;
        if (temp1.c + 1 <= C) {
            if (map[temp1.l][temp1.r][temp1.c + 1] != '#')
            {
                temp = temp1;
                temp.c ++;

                q1.push(temp);
            }
        }
        if (temp1.c - 1 > 0) {
            if (map[temp1.l][temp1.r][temp1.c - 1] != '#')
            {
                temp = temp1;
                temp.c--;

                q1.push(temp);
            }
        }
        if (temp1.r + 1 <= R) {
            if (map[temp1.l][temp1.r + 1][temp1.c] != '#')
            {
                temp = temp1;
                temp.r++;

                q1.push(temp);
            }
        }
        if (temp1.r - 1 > 0) {
            if (map[temp1.l][temp1.r - 1][temp1.c] != '#')
            {
                temp = temp1;
                temp.r--;

                q1.push(temp);
            }
        }
        if (temp1.l + 1 <= L) {
            if (map[temp1.l + 1][temp1.r][temp1.c] != '#')
            {
                temp = temp1;
                temp.l++;

                q1.push(temp);
            }
        }
        if (temp1.l - 1 > 0) {
            if (map[temp1.l - 1][temp1.r + 1][temp1.c] != '#')
            {
                temp = temp1;
                temp.l--;

                q1.push(temp);
            }
        }

    }

    if (flag == 0)
    {
        cout << "Trapped!" << endl;
    }
    else cout << "Escaped in " << ans << " minute(s)." << endl;


}
return 0;

}

  • 写回答

1条回答 默认 最新

  • shen_wei 2017-08-04 06:20
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退