您的位置:首页 > 其它

HDU 1254 推箱子 (BFS + DFS)

2016-07-12 15:37 816 查看
F - 推箱子
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d
& %I64u
Submit Status

Description

推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能推箱子而不能拉箱子,因此如果箱子被推到一个角上(如图2)那么箱子就不能再被移动了,如果箱子被推到一面墙上,那么箱子只能沿着墙移动. 

现在给定房间的结构,箱子的位置,搬运工的位置和箱子要被推去的位置,请你计算出搬运工至少要推动箱子多少格. 



 

Input

输入数据的第一行是一个整数T(1<=T<=20),代表测试数据的数量.然后是T组测试数据,每组测试数据的第一行是两个正整数M,N(2<=M,N<=7),代表房间的大小,然后是一个M行N列的矩阵,代表房间的布局,其中0代表空的地板,1代表墙,2代表箱子的起始位置,3代表箱子要被推去的位置,4代表搬运工的起始位置. 

 

Output

对于每组测试数据,输出搬运工最少需要推动箱子多少格才能帮箱子推到指定位置,如果不能推到指定位置则输出-1. 

 

Sample Input

1
5 5
0 3 0 0 0
1 0 1 4 0
0 0 1 0 0
1 0 2 0 0
0 0 0 0 0

 

Sample Output

4

 需要注意的细节问题:
1.对于方向的记录,开个三维数组即可,对于同一个位置不同方向推过来的数据都要记录
2.箱子之所以能够被推动,肯定是人在它的后面,所以在推动箱子时需要判断人从当前位置是否可以到达目的位置,如此这个地方就可以用DFS或者BFS实现(题目数据范围较小,两者看个人爱好选择)
3.如果当前位置有箱子或者墙壁,人是走不去的,换句话说,人没有穿墙术和穿箱术,箱子或者墙壁挡住了去路,也就无法过去了。

BFS中内嵌DFS代码
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#include <queue>
using namespace std;
const int MAXN = 10 + 5;
int O[MAXN][MAXN], N, M;
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
int drx[] = {-1,0,1,0};
int dry[] = {0,-1,0,1};
bool vis[MAXN][MAXN][4];
bool rvis[MAXN][MAXN];
bool sc;
int sx, sy, ex, ey, rx, ry;
struct o {
int x, y, rx, ry, b;
o() {}
o(int x, int y, int rx, int ry, int b): x(x), y(y), rx(rx), ry(ry), b(b) {}
};

queue<o> Q;
void DFS(int x, int y,int zx, int zy) {
if(x == zx && y == zy) sc = true;
if(x < 1 || y < 1 || x > N || y > M || rvis[x][y] || O[x][y]) return;
if(sc) return;
rvis[x][y] = true;
for(int i = 0; i < 4; i ++) {
DFS(x + dx[i], y + dy[i], zx, zy);
}
}

int BFS() {
memset(vis, false, sizeof(vis));
while(!Q.empty()) Q.pop();
Q.push(o(sx, sy, rx, ry, 0));
while(!Q.empty()) {
o e = Q.front();
Q.pop();
if(e.x == ex && e.y == ey) {
return e.b;
}
for(int i = 0; i < 4; i ++) {
int nx = e.x + dx[i];
int ny = e.y + dy[i];
int nrx = e.x + drx[i];
int nry = e.y + dry[i];

if(nx < 1 || ny < 1 || nx > N || ny > M || nrx < 1 || nry < 1 || nrx > N || nry > M || vis[nx][ny][i] || O[nx][ny] || O[nrx][nry]) continue;

sc = false;
memset(rvis, false, sizeof(rvis));
O[e.x][e.y] = 1;
DFS(e.rx, e.ry, nrx, nry);
O[e.x][e.y] = 0;
if(!sc) {
continue;
}
vis[nx][ny][i] = true;
Q.push(o(nx, ny, nrx, nry, e.b + 1));
}
}
return -1;
}
int main() {
int T;
scanf("%d", &T);
while(T --) {
scanf("%d%d", &N, &M);
for(int i = 1; i <= N; i ++) {
for(int j = 1; j <= M; j ++) {
scanf("%d", &O[i][j]);
if(O[i][j] == 2) sx = i, sy = j, O[i][j] = 0;
else if(O[i][j] == 3) ex = i, ey = j, O[i][j] = 0;
else if(O[i][j] == 4) rx = i, ry = j, O[i][j] = 0;
}
}
printf("%d\n",BFS());
}
return 0;
}


BFS内嵌BFS

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#include <queue>
using namespace std;
const int MAXN = 10 + 5;
int O[MAXN][MAXN], N, M;
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
int drx[] = {-1,0,1,0};
int dry[] = {0,-1,0,1};
bool vis[MAXN][MAXN][4];
bool rvis[MAXN][MAXN];
bool sc;
int sx, sy, ex, ey, rx, ry;
struct o {
int x, y, rx, ry, b;
o() {}
o(int x, int y, int rx, int ry, int b): x(x), y(y), rx(rx), ry(ry), b(b) {}
};
struct u {
int x, y;
u() {}
u(int x, int y):x(x),y(y) {}
};
queue<o> Q;
queue<u> Q2;

bool BFS2(int x, int y, int zx, int zy) {
memset(rvis, false, sizeof(rvis));
while(!Q2.empty()) Q2.pop();//注意清空
Q2.push(u(x, y));
rvis[x][y] = true;
while(!Q2.empty()) {
u e = Q2.front();
Q2.pop();
if(e.x == zx && e.y == zy) return true;
for(int i = 0; i < 4; i ++) {
int nx = e.x + dx[i];
int ny = e.y + dy[i];
if(nx < 1 || ny < 1 || nx > N || ny > M || rvis[nx][ny] || O[nx][ny]) continue;
rvis[nx][ny] = true;
Q2.push(u(nx, ny));
}
}
return false;
}
int BFS() {
memset(vis, false, sizeof(vis));
while(!Q.empty()) Q.pop();//注意清空
Q.push(o(sx, sy, rx, ry, 0));
while(!Q.empty()) {
o e = Q.front();
Q.pop();
if(e.x == ex && e.y == ey) {
return e.b;
}
for(int i = 0; i < 4; i ++) {
int nx = e.x + dx[i];
int ny = e.y + dy[i];
int nrx = e.x + drx[i];
int nry = e.y + dry[i];

if(nx < 1 || ny < 1 || nx > N || ny > M || nrx < 1 || nry < 1 || nrx > N || nry > M || vis[nx][ny][i] || O[nx][ny] || O[nrx][nry]) continue;
O[e.x][e.y] = 1;
sc = BFS2(e.rx, e.ry, nrx, nry);
O[e.x][e.y] = 0;
if(!sc) {
continue;
}
vis[nx][ny][i] = true;
Q.push(o(nx, ny, nrx, nry, e.b + 1));
}
}
return -1;
}
int main() {
int T;
scanf("%d", &T);
while(T --) {
scanf("%d%d", &N, &M);
for(int i = 1; i <= N; i ++) {
for(int j = 1; j <= M; j ++) {
scanf("%d", &O[i][j]);
if(O[i][j] == 2) sx = i, sy = j, O[i][j] = 0;
else if(O[i][j] == 3) ex = i, ey = j, O[i][j] = 0;
else if(O[i][j] == 4) rx = i, ry = j, O[i][j] = 0;
}
}
printf("%d\n",BFS());
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: