题目意思:不用多说,就是小时候古董手机上的小游戏推箱子(好像暴露了年龄hh)。
解题思路:其实刚开始我在想BFS,但是没有考虑到对人的搜寻。最后参考了别人的思路才知道用双重bfs(hh小学渣)
对箱子进行bfs,同时对人进行bfs,考虑人是否可以到达箱子原来位置的前一个位置,如果可以则用箱子的原来位置更新人的原来位置。
还需要注意一点:人不能穿过箱子,因此对人进行BFS的时候要注意约束条件。
详情见代码(hh)
#include<iostream>
#include<queue>
#include<cstdio>
using namespace std;
const int INF=10000000;
int map[110][110];
int d[10][10];
int m,n,t;
int dx[]={0,0,1,-1};
int dy[]={-1,1,0,0};
typedef pair<int,int>P;
queue<P>q;
struct node{
int px,py;
int cx,cy;
int step;
};
bool pbfs(int px,int py,int ex,int ey,int cx,int cy){
while(!q.empty()) q.pop();
int vis[10][10]={0};
q.push(P(px,py));
d[px][py]=1;
vis[px][py]=1;
if(px==ex&&py==ey) return 1;
while(q.size()){
P h=q.front();q.pop();
if(h.first==ex&&h.second==ey) return 1;
for(int i=0;i<4;i++){
int nx=h.first+dx[i];
int ny=h.second+dy[i];
if(0<=nx&&nx<m&&0<