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

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. 【Partition List】cpp

    题目: Given a linked list and a value x, partition it such that all nodes less than x come before node ...

  2. Windows网络编程笔记4 -- Winsock 协议相关知识

     Win32平台上的Winsock编程,Winsock是一个与协议无关的接口.以下协议是我们需要了解的: 网络协议的特征包括: 1.  面向消息 2.  面向连接和无线接 3.  可靠性和次序性 4. ...

  3. CentOS7安装Code::Blocks

    在CentOS7上安装Codelocks的过程. 1.安装gcc,需要c和c++两部分,默认安装下,CentOS不安装编译器的,在终端输入以下命令即可yum install gccyum instal ...

  4. Leetcode 645.最长数对链

    最长数对链 给出 n 个数对. 在每一个数对中,第一个数字总是比第二个数字小. 现在,我们定义一种跟随关系,当且仅当 b < c 时,数对(c, d) 才可以跟在 (a, b) 后面.我们用这种 ...

  5. CLion 使用笔记(三)

    我已经在博客里面发布了好几篇 CLion 使用笔记了,没追究这是第几篇,姑且算作第三篇. 我的 CLion 是搭配了 MSYS2 和 Conan 使用的.MSYS2 提供 C++ toolchain. ...

  6. 【神题】AtCoder 028 C Min Cost Cycle

    TO BE DONE 思维题 十分巧妙的转化

  7. Mongodb学习(1)--- mongoose: Schema, Model, Entity

    Schema : 一种以文件形式存储的数据库模型骨架,不具备数据库的操作能力 Model : 由 Schema 发布生成的模型,具有抽象属性和行为的数据库操作 Entity : 由 Model 创建的 ...

  8. 关于Delphi cxGrid主从表中从表只能编辑第一条记录的问题

    在Delphi cxGrid主从表中从表只能编辑第一条记录,这个问题是由于设置主从关联字段错误造成的. 从表DBtableView2的keyfieldnames,DetailKeyFieldNames ...

  9. group by timestamp

    SELECT DATE_FORMAT( deteline, "%Y-%m-%d %H" ) , COUNT( * )  FROM test GROUP BY DATE_FORMAT ...

  10. jsonp 格式

    jQuery(document).ready(function(){ $.ajax({ type: "get", async: false, url: "http://f ...