HDU 1254
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的更多相关文章
- O - 推箱子 HDU - 1254(bfs_box + bfs_man)
O - 推箱子 HDU - 1254 推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能推箱子而不能 ...
- 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 推箱子(搜索)
我写的第一道感觉比较难的搜索 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1254 首先要推箱子的话要满足人能够在箱子旁边,而且人的对面也是可通的. ...
- hdu 1254(两个BFS) 推箱子
http://acm.hdu.edu.cn/showproblem.php?pid=1254 首先,要判断人是不是可以从4到达箱子的位置2,而且不止判断一次,因为推动箱子一步后,人的位置也会改变,所以 ...
- HDU 1254 推箱子(BFS加优先队列)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1254 推箱子 Time Limit: 2000/1000 MS (Java/Others) Me ...
- 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 ...
随机推荐
- 改变Web Browser控件IE版本
默认的webbrowser控件使用的渲染模式版本似乎是IE7,想要更改更高版本,如下: 在注册表位置 HKEY_CURRENT_USER\Software\Microsoft\Internet Exp ...
- [问题2014S11] 解答
[问题2014S11] 解答 我们先引用一下复旦高代书 P310 的习题 6, 其证明可参考白皮书 P257 的例 8.33: 习题6 设实二次型 \(f(x_1,x_2,\cdots,x_n)= ...
- Windows Store Apps, Error: The certificate specified has expired.(转)
Windows Store Apps, Error: The certificate specified has expired. 0 comments|Posted on October 7th, ...
- Android的UI设计
一.Android控件 1.TextView 属性:id.width.height.gravity(对齐方式).textSize(文字大小).textColor(文字颜色) 2.Button 属性:i ...
- 我的android学习经历40
为listview设置背景,并且不随拖动改变 <ListView android:id="@+id/list_view" android:layout_width=" ...
- C语言运算符和优先级
关于C语言运算符和优先级,经整理众多博客资料汇入自己的实战,如下: a.算术运算 C语言一共有34种运算符,包括常见的加减乘除运算. 1) 加法:+ 还可以表 ...
- hdu 4358 Boring counting dfs序+莫队+离散化
Boring counting Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others) ...
- Flex http请求
下面类支持POST和GET请求,请求数据和出错将返回 package com.sole.util { import flash.events.Event; import flash.events.HT ...
- [原创] RT7 Lite win7旗舰版精简方案
[原创] RT7 Lite win7旗舰版精简方案 墨雪SEED 发表于 2016-1-26 21:23:54 https://www.itsk.com/thread-362912-1-5.html ...
- 0010 Linux 目录操作命令
01.更改目录 cd / 返回根目录 cd ~ 返回用户根目录 cd - 返回上个操作目录目录 ,等同于cd $OLDPWD 02.查看工作目录 pwd 03.创建目录 mkdir 目录名 0 ...