http://acm.hdu.edu.cn/showproblem.php?pid=1254

暴搜,状态是四维的(箱子和人的坐标),向一个方向推箱子还要判断人能否走到推的位置,1A

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std; int n, m;
int tx, ty;
int vis[][][][],vis1[][];
int M[][]; struct node {
int x, y, px, py, step;
node() {};
node(int x, int y, int px, int py, int step) :
x(x), y(y), px(px), py(py), step(step) {
};
}; node st; int dx[]={,-,,};
int dy[]={,,,-}; struct point {
int x, y;
point(int x, int y) :
x(x), y(y) {
};
}; int bfs1(int x, int y, int px, int py) {
queue <point> q;
q.push(point(px, py));
while(!q.empty()) {
point u = q.front();
q.pop();
if(u.x == x && u.y == y) return ;
for(int i = ; i < ; i++){
int xx = u.x + dx[i];
int yy = u.y + dy[i];
if(xx < || yy < || xx >=n || yy >= m) continue ;
if(M[xx][yy] == || vis1[xx][yy]) continue;
vis1[xx][yy] = ;
q.push(point(xx, yy));
}
}
return ;
}; int bfs2() {
queue <node> q;
memset(vis, , sizeof(vis));
q.push(st);
vis[st.x][st.y][st.px][st.py] = ;
while(!q.empty()) {
node u = q.front();
q.pop();
if(u.x == tx && u.y == ty) return u.step;
for(int i = ; i < ; i++) {
int xx = u.x + dx[i];
int yy = u.y + dy[i];
int px = u.x - dx[i];
int py = u.y - dy[i];
if(xx < || yy < || xx >= n || yy >=m || px < || py < || px >= n || py >= m) continue;
if(M[xx][yy] == || M[px][py] == ) continue;
if(vis[xx][yy][px][py]) continue;
memset(vis1, , sizeof(vis1));
vis1[u.x][u.y] = ;
int flag = bfs1(px, py, u.px, u.py);
vis1[u.x][u.y] = ;
if(flag) {
vis[xx][yy][px][py] = ;
q.push(node(xx, yy, px, py, u.step + ));
}
}
}
return -;
}; int main() {
int T;
scanf("%d", &T);
while(T--) {
scanf("%d%d", &n, &m);
for(int i = ; i < n; i++) {
for(int j = ; j < m; j++) {
scanf("%d", &M[i][j]);
}
}
for(int i = ; i < n; i++) {
for(int j = ; j < m; j++) {
if(M[i][j] == )
tx = i, ty = j;
if(M[i][j] == )
st.x = i, st.y = j;
if(M[i][j] == )
st.px = i, st.py = j;
}
}
st.step = ;
printf("%d\n", bfs2());
}
return ;
}

HDU 1254的更多相关文章

  1. O - 推箱子 HDU - 1254(bfs_box + bfs_man)

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

  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 推箱子(搜索)

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

  5. hdu 1254(两个BFS) 推箱子

    http://acm.hdu.edu.cn/showproblem.php?pid=1254 首先,要判断人是不是可以从4到达箱子的位置2,而且不止判断一次,因为推动箱子一步后,人的位置也会改变,所以 ...

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

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

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

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

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

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

  9. [HDU 1254] 推箱子

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

随机推荐

  1. easyui dialog 扩展load

    $.extend($.fn.panel.methods, { showMask: function(jq, msg){ return jq.each(function(){ var pal = $(t ...

  2. 彻底删除java*

    提示:先备份重要数据 1. 移除所有 Java相关包 (Sun, Oracle, OpenJDK, IcedTea plugins, GIJ): apt-get update apt-cache se ...

  3. [问题2014A12] 复旦高等代数 I(14级)每周一题(第十四教学周)

    [问题2014A12]  设 \(A,B\) 是 \(n\) 阶方阵且满足 \(AB=BA=0\), \(\mathrm{r}(A)=\mathrm{r}(A^2)\), 证明: \[\mathrm{ ...

  4. ArrayList&LinkedList&Map&Arrays

    Java集合框架 1:集合接口 1.1:Collection接口 Collection接口是构造集合框架的基础.它声明所有类集合都将拥有的核心方法 Boolean add(Object obj) 将o ...

  5. C++字符串与转移字符

    先看以下代码: #include<iostream> #include<string> using namespace std; int main() { string str ...

  6. Cheatsheet: 2015 10.01 ~ 10.31

    .NET Publishing your ASP.NET App to Linux in 5 minutes with Docker Integrating AngularJS with ASP.NE ...

  7. jquery之remove(),detach()方法详解

    一:remove()方法 remove()函数用于从文档中移除匹配的元素. 你还可以使用选择器进一步缩小移除的范围,只移除当前匹配元素中符合指定选择器的部分元素. 与detach()相比,remove ...

  8. Linux(centos)的常用基本命令

    Linux的常用基本命令. 首先启动Linux.启动完毕后需要进行用户的登录,选择登陆的用户不同自然权限也不一样,其中“系统管理员”拥有最高权限. 在启动Linux后屏幕出现如下界面显示: …… Re ...

  9. 关于Python中输出中文的一点疑问

    #encoding=gb2312 import urllib import re def getHtml(url): page = urllib.urlopen(url) html = page.re ...

  10. PHP 小方法之 计算两个时间戳之间相差的日时分秒

    if(! function_exists ('timediff') ) { function timediff($begin_time,$end_time){ if($begin_time < ...