【HDOJ】1254 推箱子
来一发搜索。数据量比较小,这么玩儿都能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 推箱子的更多相关文章
- HDU 1254 推箱子(BFS加优先队列)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1254 推箱子 Time Limit: 2000/1000 MS (Java/Others) Me ...
- HDU 1254 推箱子 BFS
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1254 题目分析: 做这道题,感觉挺简单的,做着做着就错了20次, 我也是醉了, WA到吐的节奏啊! 思 ...
- hdu 1254 推箱子(搜索)
我写的第一道感觉比较难的搜索 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1254 首先要推箱子的话要满足人能够在箱子旁边,而且人的对面也是可通的. ...
- hdu - 1254 推箱子 (bfs+bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1254 题目意思很简单,只要思路对就好. 首先考虑搬运工能否到达推箱子的那个点,这个可以根据箱子前进方向得出搬运工 ...
- hdu.1254.推箱子(bfs + 优先队列)
推箱子 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- [HDU 1254] 推箱子
推箱子 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- HDU 1254 推箱子(BFS)
Problem Description 推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能推箱子而不 ...
- hdu 1254 推箱子(双重bfs)
题目链接 Problem Description 推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能 ...
- hdu 1254 推箱子(嵌套搜索,bfs中有dfs)
推箱子 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
随机推荐
- 关于四字节字符入库时错误的解决方案(Incorrect string value: '\xF0\x9F\x99\x8F' for column 'Reply_Content' at row 1)
1. 将表字段字符集设置成utf8mb4 2. 执行插入前执行:SET NAMES utf8mb4; 如: SET NAMES utf8mb4; INSERT test(Content) VALUES ...
- ###《Max-Margin Early Event Detectors》
Paper reading. #@author: gr #@date: 2014-03-11 #@email: forgerui@gmail.com Early Detection Abstract: ...
- iOS9开发之新增通知行为详解
苹果在iOS8发布时,收到短信时可以直接在通知栏输入文字并回复,非常炫酷,然而这一功能并未真正开放给开发者.iOS9新增了用户通知行为UIUserNotificationActionBehaviorT ...
- Java中的面向对象
Java中的面向对象 在软件开发的学习中, 我最先接触的开发语言就是java,但都是简单的函数和循环数组的应用.说道面向对象,第一次看到这个词的时候还是在C#的学习过程中,我记得当时PPT上霸气的解释 ...
- ubuntu12修改ulimit
第一步:配置/etc/security/limits.confsudo vim /etc/security/limits.conf文件尾追加 * hard nofile 40960* soft nof ...
- fedora 非root用户访问socket 没用权限
在非root用户下执行基于Libpcap库编写的应用程序时不能正常运行,原因是由于libpcap库使用raw socket的套接字.而Raw Socket的使用需要root权限,否则raw socke ...
- 1. Window环境下 - 开发环境的配置: (安装Android Studio 2.1)
0. Java简介: 1990年Sun公司预料嵌入式系统将在未来家用电器领域大显生手, 于是成立了一个由James Gosling领导的"Green计划"(首席科学家Bill Jo ...
- PHP的PSR-0命名标准
PSR是Proposing a Standards Recommendation(提出标准建议)的缩写,是由PHP Framework Interoperability Group(PHP通用性框架小 ...
- Android开发第1篇 - Android开发环境搭建
归结一下,需要进行Android开发所需要的工具或软件: Eclipse - Android是基于JAVA的开发,所以选用目前来说使用较高的Eclipse作为IDE. ADT (Android Dev ...
- MSSql ID自动增长删除数据重1开始
dbcc checkident('db_Tome1.dbo.员工信息表',reseed,0) 注:dbcc checkident('表名',reseed,0)