Dungeon Master DFS和BFS

让人想犯罪 __ 提交于 2020-01-31 01:30:46

//啊啊啊 终于被我搞定
//这个题写了快一下午
//第一次用的DFS 还是感觉DFS思路清晰
//但…超时 问了老师 这种题一般都要用BFS 我昨天做的 走迷宫 那个题 应该是比较小 我用了DFS
//这种题还是我第二次用BFS做呢
//现在我知道两个用BFS的模板了

其实和我昨天发的走迷宫真的是差不多 就是多了两个方向
套路一样

附上我在某位大侠那里看到的一个样例

输入

3 4 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

4 4 5

#####
#####
##.##
##...

#####
###S#
#.#.#
#....

#####
#####
#.###
####E

#####
#...#
...##
#....

1 2 2

##
SE

3 3 3

###
#E#
#.#

#..
.##
.#.

...
...
..S

5 2 2

##
##

#.
..

.S
..

..
..

.E
..

2 1 1

S

E

1 1 2

SE

1 5 5

#####
S####
....#
####E
##...

2 3 3

#.E
...
...

...
...
..S

2 4 4

####
#S##
....
###.

####
.###
.###
.E..

2 5 5

#####
#S..#
..#..
.###.
.###.

#####
#####
#####
#####
..E..

0 0 0
————————————————

原文链接:https://blog.csdn.net/HelloWorld10086/article/details/38323189

输出

Escaped in 11 minute(s).
Trapped!
Escaped in 4 minute(s).
Escaped in 1 minute(s).
Trapped!
Escaped in 2 minute(s).
Escaped in 1 minute(s).
Escaped in 1 minute(s).
Trapped!
Escaped in 3 minute(s).
Escaped in 5 minute(s).
Escaped in 7 minute(s).


BFS

#include <iostream>
#include <string>
#include <queue>
#include <cstring>
using namespace std;
string s[31][31];
int vis[31][31][31];
int L, n, m;
//arr 定位数组
int arr[6][3] = {{0, 0, 1}, {0, 0, -1}, {0, -1, 0}, {0, 1, 0}, {-1, 0, 0}, {1, 0, 0}};
struct node
{
    int x, y, z, t;
};
int bfs(int x, int y, int z)
{
    //t1 作为 之前的
    //t2 就是 之后的
    node t1, t2;
    vis[x][y][z] = 1;
    queue<node> q;
    q.push({x, y, z, 0});
    while (!q.empty())
    {
        t1 = q.front();
        q.pop();
        //6个走法
        for (int i = 0; i < 6; i++)
        {
            t2.x = t1.x + arr[i][0];
            t2.y = t1.y + arr[i][1];
            t2.z = t1.z + arr[i][2];
            t2.t = t1.t + 1;
            if ((t2.x >= 0 && t2.x < L) && (t2.y >= 0 && t2.y < n) && (t2.z >= 0 && t2.z < m) && ((s[t2.x][t2.y][t2.z] == 'E' || s[t2.x][t2.y][t2.z] == '.') && !vis[t2.x][t2.y][t2.z]))
            {
                //如果正好到 E
                if (s[t2.x][t2.y][t2.z] == 'E')
                {
                    return t2.t;
                }
                vis[t2.x][t2.y][t2.z] = 1;
                q.push(t2);
            }
        }
    }
    return -1;
}
int main()
{
    while (1)
    {
        int ans;
        cin >> L >> n >> m;
        memset(vis, 0, sizeof(vis));
        if (L == 0 && n == 0 && m == 0)
            break;
        //输入
        for (int i = 0; i < L; i++)
        {
            for (int j = 0; j < n; ++j)
            {
                cin >> s[i][j];
            }
        }
        //找入口
        for (int i = 0; i < L; i++)
        {
            for (int j = 0; j < n; ++j)
            {
                for (int p = 0; p < m; ++p)
                {
                    if (s[i][j][p] == 'S')
                    {
                        ans = bfs(i, j, p);
                        break;
                    }
                }
            }
        }
        if (ans == -1)
            cout << "Trapped!";
        else
            printf("Escaped in %d minute(s).", ans);
        cout << endl;
    }

    return 0;
}

DFS 超时代码

#include <iostream>
#include <vector>
#include <string>
using namespace std;
string s[100][100];
int L, n, m;
int ans = 1 << 30;
int arr[6][3] = {{0, 0, 1}, {0, 0, -1}, {0, -1, 0}, {0, 1, 0}, {-1, 0, 0}, {1, 0, 0}};
// x y z 是坐标 t是步数
void dfs(int x, int y, int z, int t)
{
    if (t >= ans)
        return;
    //遍历6种走法
    for (int i = 0; i < 6; i++)
    {
        int a, b, c;
        a = x + arr[i][0];
        b = y + arr[i][1];
        c = z + arr[i][2];
        if ((a >= 0 && a < L) && (b >= 0 && b < n) && (c >= 0 && c < m) && (s[a][b][c] == 'E' || s[a][b][c] == '.'))
        {
            //找到也就结束了
            if (s[a][b][c] == 'E')
            {
                ans = min(ans, t);
                return;
            }
            //恢复现场
            s[x][y][z] = '*';
            dfs(a, b, c, t + 1);
            s[x][y][z] = '.';
        }
    }
}
int main()
{
    cin.sync_with_stdio(false);
    while (1)
    {
        ans = 1 << 30;
        cin >> L >> n >> m;
        if (L == 0 && n == 0 && m == 0)
            break;
        for (int i = 0; i < L; i++)
        {
            for (int j = 0; j < n; ++j)
            {
                cin >> s[i][j];
            }
        }
        for (int i = 0; i < L; i++)
        {
            for (int j = 0; j < n; ++j)
            {
                for (int p = 0; p < m; ++p)
                {
                    if (s[i][j][p] == 'S')
                    {
                        dfs(i, j, p, 1);
                        break;
                    }
                }
            }
        }
        if (ans == (1 << 30))
            cout << "Trapped!";
        else
            cout << "Escaped in " << ans << " minute(" << 's' << ").";
        cout << endl;
    }

    return 0;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!