题目链接: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. ftk学习记(对话框篇)

    [声明:版权全部,欢迎转载,请勿用于商业用途.  联系信箱:feixiaoxing @163.com] 前面谈到了输入法,首先看一看效果. 上面有4个输入框,大家能够分别试试,看看效果怎样. 今天,我 ...

  2. CSDN头版头条 《近匠》 Wijmo 5 CTO:从Web到移动,我的25年编程生涯

    现年52岁的Bernardo Castilho先生是GrapeCity(中文名为葡萄城)ComponentOne公司的CTO,在与他的对话过程中.充满风趣严谨和厚重的历史感. 当作为年轻人的我们崇拜着 ...

  3. ASP.NET - List<> 绑定 DropDownList

    代码: //声明泛型 List<category> inof = new List<category>();//二级分类 //声明类使用的对象类 public class ca ...

  4. 国际化之ResourceBundle

    软件在开发时要能使它同时应对世界不同地区和国家的使用,针对不同地区和国家的访问,提供相应的,符合使用者阅读习惯的操作环境,这就必须要有国际化的概念,国际化又称为“i18n”:international ...

  5. iot 表 主键索引叶子块包含了表所有数据

    <pre name="code" class="html">iot表测试: 在create table语句后面使用organization inde ...

  6. SqlHelper初探之二

    在上一篇简单的介绍了sqlhelper的基本知识,接下来就让我们进一步学习他的实践过程. 首先:我们要明白的一件事Sqlhelper不是写出来的,而是在D层的代码中提炼出来的?那么就会反问一句“D层中 ...

  7. LintCode 推断一个二叉树树是否是还有一个二叉树的子书

    有两个不同大小的二进制树: T1 有上百万的节点: T2 有好几百的节点. 请设计一种算法.判定 T2 是否为 T1的子树. /** * Definition of TreeNode: * class ...

  8. 【Unity3D自学记录】Unity3D网络之Socket聊天室初探

    首先创建一个服务端程序,这个程序就用VS的控制台程序做即可了. 代码例如以下: using System; using System.Collections.Generic; using System ...

  9. 每天一个JavaScript实例-推断图片是否载入完毕

    <!doctype html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  10. Qt之生成Window资源文件(.rc 文件)

    简述 qmake 可以随意地自动生成一个适当填充的 Windows 资源文件.本节主要讲解如何用 qmake 处理一个 Windows 资源文件,并将其链接到一个可执行应用程序(EXE)或动态链接库( ...