头条推箱子AC代码,网易的拿过来改一下就行啦,有兴趣的看看吧

#include <iostream>
#include <vector>
#include <queue>

using std::vector;
using std::queue;
using std::cin;
using std::cout;

int st[100][100][100][100];
int x, y, bx, by, tx, ty;
int m, n;
vector<vector<char>> mm;

bool valid(int x, int y) {
    if (x >= 0 && x < m && y >= 0 && y < n && mm[x][y] != '#')return true;
    return false;
}

int main() {

    cin >> m >> n;
    mm = std::vector<std::vector<char>>(m, vector<char>(n));

    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++) {
            char t;
            cin >> t;
            if (t == 'S') {
                x = i;
                y = j;
                // cout <<x<<" "<<y<<endl;
            }
            if (t == '0') {
                bx = i;
                by = j;
            }
            if (t == 'E') {
                tx = i;
                ty = j;
            }
            mm[i][j] = t;
        }

    vector<vector<int>> next = {{-1, 0},{1,  0},{0,  1},{0,  -1}};
    queue<vector<int>> que;
    que.push({x, y, bx, by});

    st[x][y][bx][by] = 1;
    while (!que.empty()) {
        vector<int> t = (vector<int> &&) que.front();
        que.pop();
        x = t[0];
        y = t[1];
        bx = t[2];
        by = t[3];
        for (int i = 0; i < next.size(); i++) {
            int nx = x + next[i][0], ny = y + next[i][1];
            int nnx = nx + next[i][0], nny = ny + next[i][1];
            if (valid(nx, ny) && (nx != bx || ny != by) && st[nx][ny][bx][by] == 0) {
                st[nx][ny][bx][by] = st[x][y][bx][by] + 1;
                que.push({nx, ny, bx, by});
                continue;
            } else if (nx == bx && ny == by && valid(nnx, nny) && st[nx][ny][nnx][nny] == 0) {
                st[nx][ny][nnx][nny] = st[x][y][bx][by] + 1;
                if (mm[nnx][nny] == 'E') {
                    cout << st[nx][ny][nnx][nny] - 1;
                    return 0;
                }
                que.push({nx, ny, nnx, nny});
            }
        }
    }

    cout << -1;
    return 0;
}

#字节跳动#
全部评论
大佬牛逼
点赞
送花
回复
分享
发布于 2017-10-17 22:54
大佬牛逼
点赞
送花
回复
分享
发布于 2017-10-17 22:54
秋招专场
校招火热招聘中
官网直投
大神是我的编译器问题么,俩大花括号编译不过啊。。。
点赞
送花
回复
分享
发布于 2017-10-18 01:41
没毛病,我也这样玩的
点赞
送花
回复
分享
发布于 2017-10-18 09:28
膜拜一下~当时题目看的毫无思路。 看完大佬代码感觉有部分可以调整的地方:st数组的维度应该根据m,n来确定更好吧?不然对大地图来说可能出现访问越界,对小地图来说就不必要的空间浪费。 另外,我没理解错的话,大佬声明的数组st应该只有0和1两个状态吧?那用bool应该更合理一点。。
点赞
送花
回复
分享
发布于 2017-10-18 17:25

相关推荐

头像
05-27 20:32
已编辑
深度学习
工行数据中心 偏运维养老 到手可能18w
点赞 评论 收藏
转发
点赞 评论 收藏
转发
点赞 25 评论
分享
牛客网
牛客企业服务