这个题目需要注意以下几点:

1)注意界线问题,箱子和人不可以越界。

2)需要判断人是否可以到达人推箱子的指定位置。

3)不可以用箱子作为标记,因为箱子可以走原来走过的地方,我们用箱子和人推箱子的方向来进行重判。定义一个Hash[8][8][8][8]来标记。

#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<stack>
#include<queue>
using namespace std;
#define MAX_SIZE 8
bool Hash[MAX_SIZE][MAX_SIZE][MAX_SIZE][MAX_SIZE];
bool visit[MAX_SIZE][MAX_SIZE];
int map[MAX_SIZE][MAX_SIZE];
int dir[4][2] = { {0,1},{0,-1},{-1,0},{1,0} };
int N,M;
struct Point {
int x, y;
bool operator==(const Point&b)const {
return x == b.x&&y == b.y;
}
bool Isleg() {
if (x<1 || x>N || y<1 || y>M || map[x][y] == 1)
return false;
return true;
}
};
struct BOX {
Point person;
Point box;
int step;
};
int BFS();
int main() {
int i, j,T,res;
scanf("%d",&T);
while (T--) {
scanf("%d%d", &N, &M);
for (i = 1; i <= N; i++)
for (j = 1; j <= M; j++)
scanf("%d", &map[i][j]);
res = BFS();
printf("%d\n", res);
}
return 0;
}
bool DFS(Point &person, Point &des,Point &box) { //判断人是否可以到达指定位置
int k;
memset(visit, 0, MAX_SIZE*sizeof(visit[0]));
visit[person.x][person.y] = 1;
stack<Point> S;
S.push(person);
Point next, pos;
while (!S.empty()) {
pos = S.top();
S.pop();
if (pos == des)
return true;
for (k = 0; k < 4; k++) {
next = pos;
next.x += dir[k][0];
next.y += dir[k][1];
if (next.Isleg() && !(next==box) && !visit[next.x][next.y]) {
visit[next.x][next.y] = 1;
S.push(next);
}
}
}
return false;
}
Point serch(int x) {
Point res;
int i, j;
for (i = 1; i <= N; i++) {
for (j = 1; j <= M; j++)
if (x == map[i][j]) {
res.x = i;
res.y = j;
}
}
return res;
}
int BFS() {
int i, j,k,l;
for (i = 1; i < MAX_SIZE; i++)
for (j = 1; j < MAX_SIZE; j++)
for (k = 1; k < MAX_SIZE; k++)
for (l = 1; l < MAX_SIZE; l++)
Hash[i][j][l][k] = 0;
BOX B,Box;
B.person = serch(4);
B.box = serch(2);
B.step = 0;
Point des, next,peo;
des = serch(3);
queue<BOX> Q;
Q.push(B);
while (!Q.empty()) {
B = Q.front();
Q.pop();
if (B.box == des)
return B.step;
B.step++;
Box = B;
for (k = 0; k < 4; k++) {
next = peo=B.box;
next.x += dir[k][0];
next.y += dir[k][1];
peo.x -= dir[k][0];
peo.y -= dir[k][1];
if (peo.Isleg() && next.Isleg() &&DFS(B.person,peo,B.box)&&!Hash[peo.x][peo.y][next.x][next.y]) {
Hash[peo.x][peo.y][next.x][next.y] = 1;
Box.person = B.box;
Box.box = next;
Q.push(Box);
}
}
}
return -1;
}

  

hdu254 DFS+BFS的更多相关文章

  1. DFS/BFS+思维 HDOJ 5325 Crazy Bobo

    题目传送门 /* 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 在树上的路径权值都小于这两个点 DFS/BFS+思维:按照权值的大小,从小的到大的连有 ...

  2. 【DFS/BFS】NYOJ-58-最少步数(迷宫最短路径问题)

    [题目链接:NYOJ-58] 经典的搜索问题,想必这题用广搜的会比较多,所以我首先使的也是广搜,但其实深搜同样也是可以的. 不考虑剪枝的话,两种方法实践消耗相同,但是深搜相比广搜内存低一点. 我想,因 ...

  3. ID(dfs+bfs)-hdu-4127-Flood-it!

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4127 题目意思: 给n*n的方格,每个格子有一种颜色(0~5),每次可以选择一种颜色,使得和左上角相 ...

  4. [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

  5. HDU 4771 (DFS+BFS)

    Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...

  6. DFS/BFS视频讲解

    视频链接:https://www.bilibili.com/video/av12019553?share_medium=android&share_source=qq&bbid=XZ7 ...

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

    POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...

  8. [LeetCode]695. 岛屿的最大面积(DFS/BFS)、200. 岛屿数量(DFS/BFS待做/并差集待做)

    695. 岛屿的最大面积 题目 给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合.你可以假设二维矩阵的四个边缘都被 ...

  9. POJ2308连连看dfs+bfs+优化

    DFS+BFS+MAP+剪枝 题意:       就是给你一个10*10的连连看状态,然后问你最后能不能全部消没? 思路:      首先要明确这是一个搜索题目,还有就是关键的一点就是连连看这个游戏是 ...

随机推荐

  1. 使用Fiddler对Android应用进行抓包

    1.  打开Fiddler软件,效果图如下: 2. 首先,确保安装 Fiddler 的电脑和你的手机在同一局域网内,因为Fiddler只是一个代理,需要将手机的代理指向 PC 机,不能互相访问是不行的 ...

  2. python 学习分享-实战篇简单的ftp

    import socket import os import time import pickle Basedb = os.path.dirname(os.path.dirname(os.path.a ...

  3. 为什么对多线程编程这么怕?pthread,sem,mutex,process

    转自http://blog.chinaunix.net/uid-20788636-id-1841334.html 1.线程创建和退出创建线程实际上就是确定调用该线程函数的入口点,这里通常使用的函数是p ...

  4. firewalld的防火墙

    firewalld的介绍与简单应用 CentOS7的默认防火墙是firewalld,在之前使用iptables时,关闭了firewalld服务,现在反过来关闭iptables服务,打开firewall ...

  5. 【bzoj3091】城市旅行 LCT区间合并

    题目描述 输入 输出 样例输入 4 5 1 3 2 5 1 2 1 3 2 4 4 2 4 1 2 4 2 3 4 3 1 4 1 4 1 4 样例输出 16/3 6/1 题解 LCT区间合并 前三个 ...

  6. MyEclipse生成get/set注释

    我的外网链接 http://yezi-0016.iteye.com/blog/2290421

  7. 刷题总结——Collecting Bugs(poj2096)

    题目: Description Ivan is fond of collecting. Unlike other people who collect post stamps, coins or ot ...

  8. MSP430之自动增益程控放大main备份

    占位符 #include <msp430.h> #include "sys.h" #include "ps2.h" #include "1 ...

  9. C# 自动注册OCX方法

    C#开发系统时,有时候会遇到调用其他语言开发的模块.如果对方提供了OCX时,就需要注册使用,但是实时时,每个客户端都注册一遍就比较麻烦.所以需要系统第一次启动时自动注册OCX. ​ 一:C#注册OCX ...

  10. 海拔(bzoj 2007)

    Description YT市是一个规划良好的城市,城市被东西向和南北向的主干道划分为n×n个区域.简单起见,可以将YT市看作一个 正方形,每一个区域也可看作一个正方形.从而,YT城市中包括(n+1) ...