HDU- 1254 推箱子(BFS)

推箱子

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 131 Accepted Submission(s): 61
 
Problem Description
推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能推箱子而不能拉箱子,因此如果箱子被推到一个角上(如图2)那么箱子就不能再被移动了,如果箱子被推到一面墙上,那么箱子只能沿着墙移动.

现在给定房间的结构,箱子的位置,搬运工的位置和箱子要被推去的位置,请你计算出搬运工至少要推动箱子多少格.


 
Input
输入数据的第一行是一个整数T(1<=T<=20),代表测试数据的数量.然后是T组测试数据,每组测试数据的第一行是两个正整数M,N(2<=M,N<=7),代表房间的大小,然后是一个M行N列的矩阵,代表房间的布局,其中0代表空的地板,1代表墙,2代表箱子的起始位置,3代表箱子要被推去的位置,4代表搬运工的起始位置.
 
Output
对于每组测试数据,输出搬运工最少需要推动箱子多少格才能帮箱子推到指定位置,如果不能推到指定位置则输出-1.
 
Sample Input
1
5 5
0 3 0 0 0
1 0 1 4 0
0 0 1 0 0
1 0 2 0 0
0 0 0 0 0
 
Sample Output
4
 
Author
Ignatius.L & weigang Lee

 看到网上有用双重bfs,感觉挺麻烦的。。。然后用的一个bfs就搞定了。。用一个4维标记数组,标记人跟箱子是否走过这个点。
用优先队列记录步数最小的路径,到达终点后一定是最小的步数。按照人的视角进行bfs,反正碰到箱子就正常地向你走的方向推,判断一下是否越界就行了。
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <cmath>
#include <stdio.h>
using namespace std;
struct node {
    int px, py;//people_position
    int bx, by;//bos_position
    int step;
    bool operator < (const node& n) const {
        return step >= n.step;
    }
    void set(int x1, int y1, int x2, int y2, int s = 0) {
        px = x1, py = y1, bx = x2, by = y2, step = s;
    }
};
int N, M;
int start_px, start_py, end_x, end_y;
int start_bx, start_by;
int visited[10][10][10][10];
int map[10][10];
int dir[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
inline void set_visited(const node& n) {//设为已访问
    visited[n.px][n.py][n.bx][n.by] = 1;
}

inline bool check(int x, int y) {//检查是否越界
    if (x <= 0 || x > N || y > M || y <= 0 || map[x][y] == 1)
        return false;
    return true;
}

inline void init() {
    memset(visited, 0, sizeof(visited));
    memset(map, 0, sizeof(map));
}

int bfs() {
    priority_queue<node> que;
    node cur, nex;
    cur.set(start_px, start_py, start_bx, start_by, 0);
    que.push(cur);
    set_visited(cur);
    while (!que.empty()) {
        cur = que.top();
        que.pop();
            //cout << cur.px << "  " << cur.py << "  " << cur.bx << "  " << cur.by << "  " << cur.step << endl;
        for (int i = 0; i < 4; ++i) {
            nex.set(cur.px + dir[i][0], cur.py + dir[i][1], cur.bx, cur.by, cur.step);
            if (nex.px == nex.bx && nex.py == nex.by) {//人走到了箱子上(推箱子
                nex.bx += dir[i][0];
                nex.by += dir[i][1];
                if (check(nex.bx, nex.by) && !visited[nex.px][nex.py][nex.bx][nex.by]) {
                    nex.step++;
                    if (nex.bx == end_x && nex.by == end_y) {
                        return nex.step;
                    }
                    set_visited(nex);
                    que.push(nex);
                }
            } else {
                if (check(nex.px, nex.py) && !visited[nex.px][nex.py][nex.bx][nex.by]) {
                    set_visited(nex);
                    que.push(nex);
                }
            }
        }
    }
    return -1;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        init();
        cin >> N >> M;
        for (int i = 1; i <= N; ++i) {
            for (int j = 1; j <= M; ++j) {
                cin >> map[i][j];
                if (map[i][j] == 4) {
                    start_px = i;
                    start_py = j;
                } else if (map[i][j] == 2) {
                    start_bx = i;
                    start_by = j;
                } else if (map[i][j] == 3) {
                    end_x = i;
                    end_y = j;
                }
            }
        }
        cout << bfs() << endl;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值