看到童年时的游戏了,嗯,推箱子。
双重搜索,第一层bfs搜索箱子可以到达的地方求最短路,第二层搜索人是否可以到达能推动箱子的地方dfs、bfs随意用个呗,
毕竟箱子如果有一面靠墙,那相反的方向那么也就不能走了,人总不能站在墙上推箱子的撒~
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
int m,n,map[10][10];
int dir[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
struct node
{
int bx,by,step,px,py;
};
//bfs判断人是否可以到达相应的地点推动箱子
int judge(int ex,int ey,int bx,int by,int px,int py)
{
int visit[10][10];
queue<node>q;
node now,next;
memset(visit,0,sizeof(visit));
now.px = px;
now.py = py;
q.push(now);
while(!q.empty())
{
now = q.front();
if(now.px==ex&&now.py==ey)
{
return 1;
}
q.pop();
for(int i = 0;i<4;i++)
{
next.px = now.px+dir[i][0];
next.py = now.py+dir[i][1];
if(0<=next.px&&next.px<m&&0<=next.py&&next.py<n&&!visit[next.px][next.py]&&map[next.px][next.py]!=1)
{
if(next.px==bx&&next.py==by)
{
if(ex!=bx||ey!=by) //如文后倒数两个例子,当人是不可以直接穿过箱子到达箱子另一面推动箱子
{
visit[next.px][next.py] = 1;
continue;
}
}
visit[next.px][next.py] = 1;
q.push(next);
}
}
}
return 0;
}
int bfs(int bx,int by,int px,int py) //对箱子进行搜索
{
int visit[10][10][4]; //三维数组记录已访问过度数组,因为箱子从不同方向到达某一点的状态是不同的所以用三维数组来记录
queue<node>q;
node now,next;
memset(visit,0,sizeof(visit));
now.bx = bx;
now.by = by;
now.px = px;
now.py = py;
now.step = 0;
q.push(now);
while(!q.empty())
{
now = q.front();
if(map[now.bx][now.by]==3)
{
return now.step;
}
q.pop();
next.px = now.bx;
next.py = now.by;
next.step = now.step+1;
for(int i = 0;i<4;i++)
{
next.bx = now.bx+dir[i][0];
next.by = now.by+dir[i][1];
if(0<=next.bx&&next.bx<m&&0<=next.by&&next.by<n&&!visit[next.bx][next.by][i]&&map[next.bx][next.by]!=1)
{
int x,y;
x = now.bx-dir[i][0];
y = now.by-dir[i][1];
if(0<=x&&0<=y&&judge(x,y,now.bx,now.by,now.px,now.py))
{
visit[next.bx][next.by][i] = 1;
q.push(next);
}
}
}
}
return -1;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int bx,by,px,py;
scanf("%d%d",&m,&n);
for(int i = 0;i<m;i++)
{
for(int j = 0;j<n;j++)
{
scanf("%d",&map[i][j]);
if(map[i][j]==2) //记录箱子的起始位置
{
bx = i;
by = j;
}
else if(map[i][j]==4) //记录搬运工的起始位置
{
px = i;
py = j;
}
}
}
int ans;
ans = bfs(bx,by,px,py);
printf("%d\n",ans);
}
return 0;
}
/*
测试数据
100
3 3
4 0 3
0 2 1
1 1 1
3 3
4 2 0
0 0 3
0 0 0
3 3
4 0 1
0 2 3
0 1 1
6 6
0 0 0 0 0 0
1 1 0 1 1 0
0 0 2 0 0 0
1 1 0 1 1 1
0 0 0 4 0 0
0 0 0 3 0 0
6 3
0 0 0
0 0 0
0 0 0
1 2 1
0 4 0
0 0 3
*/