BFS 走迷宫 与STL的低效

2 篇文章 0 订阅

Maze(for lab)

来源:http://eden.sysu.edu.cn/m/ass/6275/
You are provided a maze(迷宫), and you need to program to find the least steps to walk from the start to the end.And you can only walk in four directions:up, down,left, right.

There will only be 5 kinds of characters in the maze.The meaning of each is as followed.

“#” is for rock, which you can not walk through.

“S” is for start.

“E” is for end.

“.” is for road.

“!” is for magma(岩浆),which you can not walk through.

Input
n,m represent the maze is a nxm maze.(n rows, m columnsm,0

#####

#S..#

#.!.#

#.#E#

#####

Output

You need to give the least steps to walk from start to the end.If it doesn’t exist, then output -1.

e.g.(for the example in input)

4

第一次的提交,用map来储存深度
#include <iostream>
#include <queue>
#include <map>

struct point {
    int x;
    int y;
    point(int _x = 0, int _y = 0): x(_x), y(_y) {}
    bool operator< (point another) {
        return x <= another.x;
    }
    void operator= (point another) {
        x = another.x;
        y = another.y;
    }
};

struct classcomp {
    bool operator() (point a, point b) {
        return a.x <= b.x;
    }
};
using namespace std;

int not_get = -1;

void step(int x, int y);
char _map[22][22] = {{0}};

int main() {
    int n_row, m_col = 0;
    int target_x, target_y = 0;
    queue<point> que;
    map<point, int, classcomp> dist;

    cin >> n_row >> m_col;
    for (int i = 0; i < n_row; i++) {
        for (int j = 0; j < m_col; j++) {
            cin >> _map[i][j];
            if ('E' == _map[i][j]) {
                target_x = i;
                target_y = j;
            }
            if ('!' == _map[i][j]) {
                _map[i][j] = '#';
            }
        }
    }

    que.push(point(target_x, target_y));
    dist[point(target_x, target_y)] = 0;
    point temp;
    point temp2;
    while (!que.empty()) {
        temp = que.front();
        que.pop();
        if ('#' != _map[temp.x][temp.y + 1]) {
            temp2 = point(temp.x, temp.y+1);
            if (_map[temp2.x][temp2.y] == 'S') {
                cout << dist[temp] + 1 << endl;
                return 0;
            } else {
                que.push(temp2);
                _map[temp2.x][temp2.y] = '#';
            }
        }

        if ('#' != _map[temp.x][temp.y - 1]) {
            temp2 = point(temp.x, temp.y-1);
            if (_map[temp2.x][temp2.y] == 'S') {
                cout << dist[temp] + 1 << endl;
                return 0;
            } else {
                que.push(temp2);
                _map[temp2.x][temp2.y] = '#';
            }
        }

        if ('#' != _map[temp.x + 1][temp.y]) {
            temp2 = point(temp.x+1, temp.y);
            if (_map[temp2.x][temp2.y] == 'S') {
                cout << dist[temp] + 1 << endl;
                return 0;
            } else {
                que.push(temp2);
                _map[temp2.x][temp2.y] = '#';
            }
        }

        if ('#' != _map[temp.x - 1][temp.y]) {
            temp2 = point(temp.x-1, temp.y);
            if (_map[temp2.x][temp2.y] == 'S') {
                cout << dist[temp] + 1 << endl;
                return 0;
            } else {
                que.push(temp2);
                _map[temp2.x][temp2.y] = '#';
            }
        }
    }
    cout << not_get << endl;
    return 0;
}

第一次提交用map来储存深度,超时了。
第二次改把深度放到结构体中一起储存,就是把

struct point {
    int x;
    int y;
    point(int _x = 0, int _y = 0): x(_x), y(_y) {}
//    bool operator< (point another) {
//        return x <= another.x;
    }
    void operator= (point another) {
        x = another.x;
        y = another.y;
    }
};

改为了

struct point {
    int x;
    int y;
    int dep;
    point(int _x = 0, int _y = 0, int _dep = 0): x(_x), y(_y), dep(_dep) {}
//    bool operator< (point another) {
//        return x <= another.x;
//    }
    void operator= (point another) {
        x = another.x;
        y = another.y;
        dep = another.dep;
    }
};

整体的代码变为:

#include <iostream>
#include <queue>
#include <map>

struct point {
    int x;
    int y;
    int dep;
    point(int _x = 0, int _y = 0, int _dep = 0): x(_x), y(_y), dep(_dep) {}
//    bool operator< (point another) {
//        return x <= another.x;
//    }
    void operator= (point another) {
        x = another.x;
        y = another.y;
        dep = another.dep;
    }
};

using namespace std;

int not_get = -1;

void step(int x, int y);
char _map[22][22] = {{0}};

int main() {
    int n_row, m_col = 0;
    int target_x, target_y = 0;
    queue<point> que;

    cin >> n_row >> m_col;
    for (int i = 0; i < n_row; i++) {
        for (int j = 0; j < m_col; j++) {
            cin >> _map[i][j];
            if ('E' == _map[i][j]) {
                target_x = i;
                target_y = j;
            }
            if ('!' == _map[i][j]) {
                _map[i][j] = '#';
            }
        }
    }

    que.push(point(target_x, target_y, 0));
    _map[target_x][target_y] = '#';
    point temp;
    point temp2;
    while (!que.empty()) {
        temp = que.front();
        que.pop();
        if ('#' != _map[temp.x][temp.y + 1]) {
            temp2 = point(temp.x, temp.y+1, temp.dep + 1);
            if (_map[temp2.x][temp2.y] == 'S') {
                cout << temp2.dep << endl;
                return 0;
            } else {
                que.push(temp2);
                _map[temp2.x][temp2.y] = '#';
            }
        }

        if ('#' != _map[temp.x][temp.y - 1]) {
            temp2 = point(temp.x, temp.y-1, temp.dep + 1);
            if (_map[temp2.x][temp2.y] == 'S') {
                cout << temp2.dep << endl;
                return 0;
            } else {
                que.push(temp2);
                _map[temp2.x][temp2.y] = '#';
            }
        }

        if ('#' != _map[temp.x + 1][temp.y]) {
            temp2 = point(temp.x+1, temp.y, temp.dep + 1);
            if (_map[temp2.x][temp2.y] == 'S') {
                cout << temp2.dep << endl;
                return 0;
            } else {
                que.push(temp2);
                _map[temp2.x][temp2.y] = '#';
            }
        }

        if ('#' != _map[temp.x - 1][temp.y]) {
            temp2 = point(temp.x-1, temp.y, temp.dep + 1);
            if (_map[temp2.x][temp2.y] == 'S') {
                cout << temp2.dep << endl;
                return 0;
            } else {
                que.push(temp2);
                _map[temp2.x][temp2.y] = '#';
            }
        }
    }
    cout << not_get << endl;
    return 0;
}

完美地通过了所有测例。

结论

STL效率相对低下,如非万不得已,尽量少用STL

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值