hdu254 DFS+BFS

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

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;
}

  

时间: 2024-10-25 13:25:07

hdu254 DFS+BFS的相关文章

Dearboy&#39;s Puzzle (poj 2308 搜索 dfs+bfs)

Language: Default Dearboy's Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1202   Accepted: 208 Description Dearboy is a game lover. Recently, he loves playing the game Lian Lian Kan. This game is played on a board with N*M grids

FZU1205/SDUT1157_小鼠迷宫问题(DFS+BFS)

解题报告 http://blog.csdn.net/juncoder/article/details/38146041 题目传送门 题意 求最短路和最短路的路数. 思路: BFS+DFS,先求出最短路.在DFS搜等于最短路的条数. 不加优化SDUTOJ过了,数据就是水. 确定了最短路的长度,加上奇偶剪枝FOJ也过了. #include <queue> #include <cmath> #include <cstdio> #include <cstring>

poj3083——dfs+bfs综合题

POJ 3083   dfs+bfs+模拟 Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10564   Accepted: 4539 Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through

POJ 3083:Children of the Candy Corn(DFS+BFS)

Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9311 Accepted: 4039 Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, ch

HDU 4771 (DFS+BFS)

Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and his owl. When Hogwarts school is in holiday, Harry Potter has to go back to uncle Vernon's home. But he can't bring his precious with him. As you know,

HDU 4771 Stealing Harry Potter&#39;s Precious dfs+bfs

Stealing Harry Potter's Precious Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and his owl. When Hogwarts school is in holiday, Harry Potter has to go back to uncle Vernon's home. But he can't bring his

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

[题目链接:NYOJ-58] 经典的搜索问题,想必这题用广搜的会比较多,所以我首先使的也是广搜,但其实深搜同样也是可以的. 不考虑剪枝的话,两种方法实践消耗相同,但是深搜相比广搜内存低一点. 我想,因为广搜需要的就是队列,所以相比递归队列更耗内存? 当然DFS并不像上图所说,需要用栈,而是运用递归即可. BFS: 因为BFS是要一个接一个的遍历,所以用到了结构体,来保存坐标和当前所走步数 1.每走一步,通过定义的结构体,从队列中提取a(即上一步的坐标.步数(步数每次累加)) 2.在a的基础上进行

DFS/BFS+思维 HDOJ 5325 Crazy Bobo

题目传送门 1 /* 2 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 3 在树上的路径权值都小于这两个点 4 DFS/BFS+思维:按照权值的大小,从小的到大的连有向边,搜索最多连接点数即是答案.因为排序后,他们之间的路径, 5 可定都是从当前节点u连过去的,那么都是小于这两个节点的.DFS需手动加栈,BFS类似拓扑排序的思路 6 */ 7 #pragma comment (linker, "/STACK:1024000000,10240000

Dfs/Bfs/记忆化搜索问题 | 问题集合

写在前面 动归和搜索似乎我打得特憋懒. 可能是因为搜索打的太少了??? 然后之前做过的一些题我就不再写了,比如填涂颜色/海战啥的? 然后每一题打两种解法(:Dfs/Bfs 前提是在题目里两种都能A P1596 湖计数 题目描述 Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <=