来一发搜索。数据量比较小,这么玩儿都能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. IOS-7步学会用代理

    代理:又叫委托 自己不能去办的事委托给别人去办 之前学过的 UIAlertView UITextField都是使用了代理 反向传值代理 代理Block 写代理的步骤 需要帮忙的人(请求帮代饭的人) 1 ...

  2. 12天学好C语言——记录我的C语言学习之路(Day 9)

    12天学好C语言--记录我的C语言学习之路 Day 9: 函数部分告一段落,但是我们并不是把函数完全放下,因为函数无处不在,我们今后的程序仍然会大量运用到函数 //转入指针部分的学习,了解指针是什么 ...

  3. poj 3565 uva 1411 Ants KM算法求最小权

    由于涉及到实数,一定,一定不能直接等于,一定,一定加一个误差<0.00001,坑死了…… 有两种事物,不难想到用二分图.这里涉及到一个有趣的问题,这个二分图的完美匹配的最小权值和就是答案.为啥呢 ...

  4. 基本上,把switch,用设计模式代替,肯定是bug和过度设计。想想,本来修改一个文件几行代码可以解决的问题,变成修改3-6个类才能实现一样的功能。不是傻是什么?

    那些迷信设计模式的人,来修改一下这个方法吧.看看你最终的代码膨胀为几倍... public virtual PasswordChangeResult ChangePassword(ChangePass ...

  5. Spring MVC中Ajax实现二级联动

    今天写项目遇到了二级联动,期间遇到点问题,写个博客记录一下. 后台Controller: @RequestMapping("/faultType") @ResponseBody p ...

  6. bat文件的妙用1-一键开启所有开发软件

    每天早上来的第一件事情,就是打开电脑,然后开一堆的软件 1.wamp 开发环境 2.钉钉   通讯工具 3.PHPstrom 开发工具 4.nodejs.bat Nodejs的扩展(node D:/w ...

  7. 云盾正常扫描云服务器的IP是什么

    问题:云盾正常扫描云服务器的IP是什么?   解答:云盾扫描云服务器的的IP段固定为    42.120.145.0/24 110.75.105.0/24 110.75.185.0/24 110.75 ...

  8. Linux C 程序 两变量值交换(FIVE)

    example:1.运算符: #include<stdio.h> int main(){ int a , b , c ,d ; a = ; b = a++;//先赋值给b,a再自增 c = ...

  9. RX学习笔记:Bootstrap

    Bootstrap https://getbootstrap.com 2016-07-01 在学习FreeCodeCamp课程中了解到Bootstrap,并于课程第一个实战题卡在响应式部分,于是先对B ...

  10. win7定时任务

    最近某app有个小抽奖,每天点击太麻烦,想做个定时任务访问抽奖链接(带着cookie登录信息,即可抽奖成功) 刚开始做了php,最后发现部署在百度bae中没法做定时任务每天执行,后来换成了python ...