来一发搜索。数据量比较小,这么玩儿都能ac。搞个优先队列。
先扫描从起点可以到达箱子的上下左右中哪些位置,并针对这些位置进行bfs。
所谓推,就是箱子和人同向移动一个单位。bfs的时候注意一些限制条件就好了。

 /* 1254 */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int maxn = ;
int M[maxn][maxn];
bool reach[maxn][maxn];
bool visit[maxn][maxn][maxn][maxn];
int dir[][] = {
-,, ,, ,-, ,
};
int gx, gy, bx, by, hx, hy;
int n, m;
int ans; bool judge(int x, int y) {
return x< || x>=n || y< || y>=m;
} void bfs_human() {
queue<pii> Q;
pii p;
int x, y; memset(reach, false, sizeof(reach));
reach[hx][hy] = true;
Q.push(mp(hx, hy)); while (!Q.empty()) {
p = Q.front();
Q.pop();
rep(i, , ) {
x = p.fir + dir[i][];
y = p.sec + dir[i][];
if (judge(x, y) || M[x][y]!= || reach[x][y])
continue;
reach[x][y] = true;
Q.push(mp(x, y));
}
}
} typedef struct node_t {
int hx, hy;
int bx, by;
int bn; node_t() {} node_t(int hx, int hy, int bx, int by, int bn):
hx(hx), hy(hy), bx(bx), by(by), bn(bn) {} friend bool operator< (const node_t& a, const node_t& b) {
return a.bn > b.bn;
} void print() {
printf("hx=%d, hy=%d, bx=%d, by=%d, bn=%d\n", hx,hy,bx,by,bn);
} } node_t; // #define DEBUG
int bfs(int hx, int hy) {
priority_queue<node_t> Q;
node_t d(hx, hy, bx, by, ), nd; memset(visit, false, sizeof(visit));
visit[bx][by][hx][hy] = true;
Q.push(d); while (!Q.empty()) {
nd = Q.top();
Q.pop();
#ifdef DEBUG
nd.print();
if (nd.hx== && nd.hy== && nd.bx== && nd.by==) {
puts("here");
}
#endif
rep(i, , ) {
d.hx = nd.hx + dir[i][];
d.hy = nd.hy + dir[i][];
if (judge(d.hx, d.hy) || M[d.hx][d.hy]==)
continue; // check whether is box
if (d.hx==nd.bx && d.hy==nd.by) {
d.bx = nd.bx + dir[i][];
d.by = nd.by + dir[i][];
if (judge(d.bx, d.by) || M[d.bx][d.by]==)
continue;
d.bn = nd.bn + ;
} else {
d.bx = nd.bx;
d.by = nd.by;
d.bn = nd.bn;
} if (visit[d.bx][d.by][d.hx][d.hy])
continue; visit[d.bx][d.by][d.hx][d.hy] = true;
if (d.bx==gx && d.by==gy)
return d.bn; #ifdef DEBUG
nd.print();
if (d.hx== && d.hy== && d.bx== && d.by==) {
puts("there");
}
#endif
Q.push(d);
}
} return INT_MAX;
} void solve() {
int x, y, tmp; ans = INT_MAX; bfs_human();
rep(i, , ) {
x = bx + dir[i][];
y = by + dir[i][];
if (judge(x, y) || M[x][y]== || !reach[x][y])
continue;
tmp = bfs(x, y);
ans = min(tmp, ans);
} ans = ans==INT_MAX ? -:ans;
printf("%d\n", ans);
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int t; scanf("%d", &t);
while (t--) {
scanf("%d %d", &n, &m);
rep(i, , n) {
rep(j, , m) {
scanf("%d", &M[i][j]);
if (M[i][j] == ) {
bx = i;
by = j;
} else if (M[i][j] == ) {
gx = i;
gy = j;
M[i][j] = ;
} else if (M[i][j] == ) {
hx = i;
hy = j;
M[i][j] = ;
}
}
} solve();
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}

【HDOJ】1254 推箱子的更多相关文章

  1. HDU 1254 推箱子(BFS加优先队列)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1254 推箱子 Time Limit: 2000/1000 MS (Java/Others)    Me ...

  2. HDU 1254 推箱子 BFS

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1254 题目分析: 做这道题,感觉挺简单的,做着做着就错了20次, 我也是醉了, WA到吐的节奏啊! 思 ...

  3. hdu 1254 推箱子(搜索)

    我写的第一道感觉比较难的搜索 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1254 首先要推箱子的话要满足人能够在箱子旁边,而且人的对面也是可通的. ...

  4. hdu - 1254 推箱子 (bfs+bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1254 题目意思很简单,只要思路对就好. 首先考虑搬运工能否到达推箱子的那个点,这个可以根据箱子前进方向得出搬运工 ...

  5. hdu.1254.推箱子(bfs + 优先队列)

    推箱子 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  6. [HDU 1254] 推箱子

    推箱子 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  7. HDU 1254 推箱子(BFS)

    Problem Description 推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能推箱子而不 ...

  8. hdu 1254 推箱子(双重bfs)

    题目链接 Problem Description 推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能 ...

  9. hdu 1254 推箱子(嵌套搜索,bfs中有dfs)

    推箱子 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

随机推荐

  1. [leetcode] 407. Trapping Rain Water II

    https://leetcode.com/contest/6/problems/trapping-rain-water-ii/ 看到这题,我很高兴,因为我做过!哈哈!其实我现在也写不出来,知道大概思想 ...

  2. 暑假集训(3)第一弹 -----还是畅通工程(hdu1233)

    题意梗概:N(n<100)个村子想要富起来,自然就要先修路,不过到底还是没富起来,所以陷入了一个怪圈 :资金不足->修不起路->资金不足...... 为了实现走向全民小康社会,全面实 ...

  3. Curl的编译

    下载 curl的官网:https://curl.haxx.se/ libcurl就是一个库,curl就是使用libcurl实现的. curl是一个exe,也可以说是整个项目的名字,而libcurl就是 ...

  4. windows 安装 setuptools

    在python的网站上 : https://pypi.python.org/pypi/setuptools/ 查找windows,显不如下: 点击 ez_setup.py进入, 并将内容复制下来, 保 ...

  5. CentOS 6.5升级Python后yum不可用的解决方案

    因开发需要,今天把CentOS 6.5自带的Python2.6.6升级到了Python2.7.3.按照如下步骤进行升级 1.查看当前系统python的版本 python -V 2.下载2.7.3版本的 ...

  6. Extension method for type

    扩展其实真的很简单 msdn是这样规定扩展方法的:"扩展方法被定义为静态方法,但它们是通过实例方法语法进行调用的. 它们的第一个参数指定该方法作用于哪个类型,并且该参数以 this 修饰符为 ...

  7. Linux下实现流水灯等功能的LED驱动代码及测试实例

    驱动代码: #include <linux/errno.h> #include <linux/kernel.h> #include <linux/module.h> ...

  8. SQL函数说明大全 (转)

    一旦成功地从表中检索出数据,就需要进一步操纵这些数据,以获得有用或有意义的结果.这些要求包括:执行计算与数学运算.转换数据.解析数值.组合值和聚合一个范围内的值等. 下表给出了T-SQL函数的类别和描 ...

  9. Vijos P1003 等价表达式 随机数+单调栈

    题目链接:https://vijos.org/p/1003 题意: 1. 表达式只可能包含一个变量‘a’. 2. 表达式中出现的数都是正整数,而且都小于10000. 3. 表达式中可以包括四种运算‘+ ...

  10. 魔兽争霸Ⅲ运行时不能初始化directX的错误解决

    运行魔兽争霸3不能初始化DirectX错误这样解决: 1:在运行中输入(win+r):dxdiag,查看显示栏,确定电脑已安装好directx 8.1以上,且下面的三个加速都已开启. 2:如果没有安装 ...