题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1254

分析:

真正移动的是箱子,但是要移动箱子需要满足几个条件。

1.移动方向上没有障碍。

2.箱子后方没有障碍。

3.人可以到达箱子后方的地方。这里dfs或bfs都可以实现

按条件搜索即可。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 1000000007
#define inf 0x3f3f3f3f
#define N 100010
using namespace std;
typedef struct node
{
int Bx,By;
int Mx,My;
int step;
bool operator<(const node &a)const
{
return step>a.step;
}
}node;
int dx[]={,,,-};
int dy[]={,-,,};
int hash[][][][];
int vis[][],s[][];
int Bx,By,Mx,My,Nx,Ny;
int found,n,m;
node make_node(int a,int b,int x,int y)
{
node temp;
temp.Bx=a;temp.By=b;
temp.Mx=x;temp.My=y;
temp.step=;
return temp;
} int judge(int x,int y)
{
return x>=&&x<n&&y>=&&y<m&&s[x][y]!=;
}
void dfs(int Nx,int Ny,int Mx,int My)
{
if(Nx==Mx&&Ny==My)
{
found=;return;
}
for(int i=;i<&&!found;i++)
{
int x=Nx+dx[i];
int y=Ny+dy[i];
if(judge(x,y)&&!vis[x][y])
vis[x][y]=,dfs(x,y,Mx,My);
}
} void bfs(int Bx,int By,int Mx,int My)
{
priority_queue<node>que;
node p,q;
p=make_node(Bx,By,Mx,My);
que.push(p);
while(!que.empty())
{
node p=que.top();que.pop();
if(s[p.Bx][p.By]==)
{
printf("%d\n",p.step);return;
}
for(int i=;i<;i++)
{
q=p;
q.Bx=p.Bx+dx[i];//箱子移动的地方
q.By=p.By+dy[i];
Nx=p.Bx-dx[i];//箱子的后方
Ny=p.By-dy[i];
if(judge(q.Bx,q.By)&&judge(Nx,Ny)&&!hash[q.Bx][q.By][Nx][Ny])
{
memset(vis,,sizeof(vis));
vis[p.Bx][p.By]=vis[Nx][Ny]=;//注意这里箱子将成为阻碍物
found=;
dfs(Nx,Ny,p.Mx,p.My);//判断人是否可达箱子后面
if(found)
{
hash[q.Bx][q.By][Nx][Ny]=;
q.Mx=p.Bx;q.My=p.By;q.step++;
que.push(q);
}
}
}
}
printf("-1\n");
return;
}
void init()
{
memset(hash,,sizeof(hash));
memset(s,,sizeof(s));
for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
scanf("%d",&s[i][j]);
if(s[i][j]==)
{
Bx=i;By=j;
}
if(s[i][j]==)
{
Mx=i;My=j;
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
init();
bfs(Bx,By,Mx,My);
}
}

hdu1254(bfs+dfs)的更多相关文章

  1. Cleaning Robot (bfs+dfs)

    Cleaning Robot (bfs+dfs) Here, we want to solve path planning for a mobile robot cleaning a rectangu ...

  2. hdu 4771 求一点遍历全部给定点的最短路(bfs+dfs)

    题目如题.题解如题. 因为目标点最多仅仅有4个,先bfs出俩俩最短路(包含起点).再dfs最短路.)0s1A;(当年弱跪杭州之题,现看如此简单) #include<iostream> #i ...

  3. HDU1254--推箱子(BFS+DFS)

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s) ...

  4. 图的基本遍历算法的实现(BFS & DFS)复习

    #include <stdio.h> #define INF 32767 typedef struct MGraph{ ]; ][]; int ver_num, edge_num; }MG ...

  5. HDU 1044 Collect More Jewels(BFS+DFS)

    Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  6. 图的遍历(bfs+dfs)模板

    bfs #include<iostream> #include<queue> #include<cstdio> using namespace std; queue ...

  7. POJ-3083 Children of the Candy Corn (BFS+DFS)

    Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and mus ...

  8. POJ 3126 Prime Path(BFS算法)

    思路:宽度优先搜索(BFS算法) #include<iostream> #include<stdio.h> #include<cmath> #include< ...

  9. Addition Chains POJ - 2248 (bfs / dfs / 迭代加深)

    An addition chain for n is an integer sequence <a0, a1,a2,...,am=""> with the follow ...

随机推荐

  1. ActivityManager

    android.app.ActivityManager 这个类主要用来管理全部设备上的Activities. 权限:android.permission.GET_TASKS 方法:| 返回类型     ...

  2. Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境(二)

    上一篇随笔Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境(一)介绍maven和nexus的环境搭建,以及如何使用maven和nexus统一管理库文件和版本,以 ...

  3. webstorm入门1-主题和配色

    1.引子 以前介绍过 Sublime text 3 系列的文章,着重介绍了 Sublime text 3 如何下载.安装.插件.配置等内容.Sublime text 3的轻量和富扩展,为前端开发带来了 ...

  4. PNG 文件结构

    PNG图像文件介绍 PNG图像文件格式 PNG是可携式网络图像(portable network graphics)的英文缩写.PNG是从网络上开始发展的,目的是替代GIF和JPG格式,PNG图像文件 ...

  5. QNX---- interrupts 例程

    #include <sys/neutrino.h> int interruptID; const struct sigevent * intHandler (void *arg, int ...

  6. Matlab的parfor并行编程

    Matlab的parfor并行编程 通常消耗最多计算资源的程序往往是循环. 把循环并行化.或者优化循环体中的代码是最经常使用的加快程序执行速度的思路. Matlab提供了parforkeyword,能 ...

  7. 2014Esri全球用户大会——亮点系列之精彩应用案例

    在2014 Esri UC上,Esri邀请不少用户到场分享了自己企业的案例,在这里与大家进行分享. 一.City of Mineapolis 1.案例背景 Mineapolis市使用GIS已经数十年, ...

  8. Mac下添加java环境变量

    2015年4月22号更新: 发现一个坑:最近发现有同事按照本文方式配置jdk环境变量一直不成功,后来发现他是使用了“Oh-My-Zsh”,配置文件的路径不是/etc/profile或~/.bash_p ...

  9. 【从零学习经典算法系列】分治策略实例——高速排序(QuickSort)

    在前面的博文(http://blog.csdn.net/jasonding1354/article/details/37736555)中介绍了作为分治策略的经典实例,即归并排序.并给出了递归形式和循环 ...

  10. IOS之【地图MapKit】

    iOS地图位置开发   iPhone SDK提供了三个类来管理位置信息:CLLocation CLLocationManager 和 CLLHeading(不常用).除了使用GPS来获取当前的位置信息 ...