hdu.1254.推箱子(bfs + 优先队列)
推箱子
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6021 Accepted Submission(s): 1718
现在给定房间的结构,箱子的位置,搬运工的位置和箱子要被推去的位置,请你计算出搬运工至少要推动箱子多少格.

5 5
0 3 0 0 0
1 0 1 4 0
0 0 1 0 0
1 0 2 0 0
0 0 0 0 0
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
int T ;
int n , m ;
const int M = ;
int map[M][M] ;
bool vis[M][M][M][M] ;
int move[][] = {{,} , {- , } , {,} , { , -} } ;
struct node
{
int x , y ;
int a , b ;
int time ;
bool operator < (const node &rhs ) const
{
return time > rhs.time ;
}
}; int bfs (int sx , int sy , int mx , int my , int ex , int ey)
{
//printf ("Last---> (%d,%d)\n" , ex , ey ) ;
node ans , tmp ;
std::priority_queue<node> q ;
memset (vis , , sizeof(vis)) ;
while ( !q.empty ()) q.pop () ;
q.push ( (node) {sx , sy , mx , my , }) ;
vis[sx][sy][mx][my] = ;
if (mx == ex && my == ey) return ;
while ( !q.empty ()) {
ans = q.top () ; q.pop () ;
// printf ("S----(%d,%d) tui (%d,%d)\n" , ans.x , ans.y , ans.a , ans.b ) ;
for (int i = ; i < ; i ++) {
tmp = ans ;
tmp.x += move[i][] ; tmp.y += move[i][] ;
if (tmp.x < || tmp.y < || tmp.x == n || tmp.y == m) continue ;
if (map[tmp.x][tmp.y] == ) continue ;
if (tmp.x == tmp.a && tmp.y == tmp.b ) {
int x = tmp.x + move[i][] , y = tmp.y + move[i][] ;
if (x < || y < || x == n || y == m) continue ;
if (map[x][y] == ) continue ;
tmp.a = x , tmp.b = y ;
tmp.time ++ ;
}
if (vis[tmp.x][tmp.y][tmp.a][tmp.b]) continue ;
vis[tmp.x][tmp.y][tmp.a][tmp.b] = ;
q.push (tmp ) ;
// printf ("(%d,%d) tui (%d,%d)\n" , tmp.x , tmp.y , tmp.a , tmp.b ) ;
if (tmp.a == ex && tmp.b == ey) return tmp.time ;
}
}
return - ;
} int main ()
{
//freopen ("a.txt" , "r" , stdin ) ;
scanf ("%d" , &T ) ;
while (T --) {
scanf ("%d%d" , &n , &m ) ;
int k ;
int sx , sy , ex , ey , mx , my ;
for (int i = ; i < n ; i ++) for (int j = ; j < m ; j ++) scanf ("%d" , &map[i][j]) ;
for (int i = ; i < n ; i ++) {
for (int j = ; j < m ; j ++) {
if (map[i][j] == ) sx = i , sy = j ;
else if (map[i][j] == ) mx = i , my = j ;
else if (map[i][j] == ) ex = i , ey = j ;
}
}
if ( (k = bfs (sx , sy , mx , my , ex , ey )) == -) puts ("-1") ;
else printf ("%d\n" , k ) ;
}
return ;
}
[ Copy to Clipboard ] [ Save to File]
hdu.1254.推箱子(bfs + 优先队列)的更多相关文章
- HDU 1254 推箱子 BFS
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1254 题目分析: 做这道题,感觉挺简单的,做着做着就错了20次, 我也是醉了, WA到吐的节奏啊! 思 ...
- hdu - 1254 推箱子 (bfs+bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1254 题目意思很简单,只要思路对就好. 首先考虑搬运工能否到达推箱子的那个点,这个可以根据箱子前进方向得出搬运工 ...
- HDU 1254 推箱子(BFS加优先队列)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1254 推箱子 Time Limit: 2000/1000 MS (Java/Others) Me ...
- 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 ...
- hdu 1254 推箱子(搜索)
我写的第一道感觉比较难的搜索 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1254 首先要推箱子的话要满足人能够在箱子旁边,而且人的对面也是可通的. ...
- [HDU 1254] 推箱子
推箱子 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- HDU 1254 推箱子游戏(搞了一下午。。。)
中文题目:http://acm.hdu.edu.cn/showproblem.php?pid=1254 一开始常规的人用来做主导,想着想着不对劲,其实是箱子为主导,人只是箱子能否推进的一个判断. 可以 ...
随机推荐
- 判断请求是不是ajax
public static bool IsAjaxRequest(HttpRequest request) { if (request == null) { throw new ArgumentNul ...
- CloseHandle(IntPtr handle)抛异常
[DllImport("Kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static ext ...
- hdu 2042 - 不容易系列之二
题意:(略)太长了 解法:找规律,发现A[i]=A[i-1]*2-2; 1: #include<stdlib.h> 2: #include<string.h> 3: #incl ...
- STM8L --- External interrupt
note 1: Several interrupts can be pending at the same time. When an interrupt request is not servic ...
- 如何用iframe标签以及Javascript制作时钟?
如何利用iframe标签以及Javascript制作时钟? 如何制作一个时钟呢?效果如下图所示: 这里的时钟会不停的走,但是下面的页面是不会变得,这样就实现了我们想要的效果了.下面我将分为以下几个方面 ...
- [EmguCV|WinForm] 使用EmguCV內建直方圖工具繪製直方圖(Histogram)-直方圖(Histogram)系列 (1)
https://dotblogs.com.tw/v6610688/archive/2013/12/20/emgucv_draw_histogram_histogrambox_histogramview ...
- webView(简单的浏览器)
#import "MJViewController.h" @interface MJViewController () <UISearchBarDelegate, UIWeb ...
- 【原】js 签到用日历
最近做的一个项目中,需要用到一个日历来记录你的签到,网上找了一些,感觉挺庞大的,所以就自己写了一个,记录一下自己写这个日历的经过 html代码: <table cellspacing=" ...
- 获取jsp页面的宽和高
var winWidth; var winHeight; function getResult() { if(window.innerWidth) { winWidth=window.innerWid ...
- arcglobe 图层三大类说明
若是第一次打开,且在网络连接通畅的情况下,你会发现目录中已有部分数据层,这些数据层是由ArcGIS Online的在线数据:Imagery图层即在线的影像数据.高程数据.地名数据.运输线数据. Arc ...