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
BFS+优先队列
思路
考虑 人比箱子先走一步 {
1.如果人走到箱子的位置 说明人能够推动箱子
2. 如果箱子下一步的位置 符合界限 不是障碍的要求 则该点可以实现推一格
3. vis数组判断这种情形是否遇到过(关键点从不同的位置推箱子产生后续的状态也是不同的)}
4. 如果人走不到箱子的位置那么说明step 没有必要+1
上代码~
解释 : 结构体存的是人的位置,箱子的位置,箱子的移动数目
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
int n,m,mp[10][10],vis[10][10][10][10];
struct node{
int x,y,xx,xy,step;
bool friend operator <(const node &a,const node &b){
return a.step >b.step ;
}
}st;
int to[4][2]={1,0,0,1,-1,0,0,-1};
int check(int x,int y){
if(x<0||x>=n||y<0||y>=m||mp[x][y]==1) return 0;
return 1;
}
void bfs( ){
priority_queue<node>h;
h.push(st);
vis[st.x ][st.y ][st.xx ][st.xy] =1;
while(!h.empty() ){
node now,net;
now=h.top() ;
h.pop() ;
if(mp[now.xx ][now.xy ]==3) {
printf("%d\n",now.step );
return ;
}
int i;
for(i=0;i<4;i++){
net=now;
net.x =now.x +to[i][0];
net.y=now.y +to[i][1];
if(!check(net.x ,net.y )) continue;
if(net.x ==now.xx &&net.y ==now.xy ){
net.xx=net.x+to[i][0];
net.xy=net.y+to[i][1];
if(!check(net.xx ,net.xy )){
continue;
}
net.step ++;
}
if(!vis[net.x ][net.y ][net.xx ][net.xy ]){
vis[net.x ][net.y ][net.xx ][net.xy ]=1;
h.push(net);
}
}
}
puts("-1");
return ;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
int i,j;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
scanf("%d",&mp[i][j]);
if(mp[i][j]==4){
st.x =i;
st.y =j;
}else if(mp[i][j]==2){
st.xx =i;
st.xy =j;
}
}
}
memset(vis,0,sizeof(vis));
st.step =0;
bfs();
}
}
以下是我最开始时候的思路{
先考虑人是否能到箱子的初始位置;
继续考虑是否能移动到目标位置;
} 蒟蒻觉得自己是{
因为没有结合考虑 只判断人能够到达宝箱的位置而没有考虑人能不能到达宝箱后推的那个点也就是说虽然我判断那个点的界限障碍问题而没有考虑人是否能到达此点的情况(路上是否可走
}WA
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
int m,n;
int map[10][10];
int vis[10][10];
int rx,ry,tx,ty,sx,sy;
int to[4][2]={1,0,0,1,-1,0,0,-1};
struct node{
int x;
int y;
};
int check(int x,int y){
if(x<1||x>m||y<1||y>n||map[x][y]==1) return 0;
return 1;
}
int jiaoluo(int x,int y){
if(x==1&&y==1) return 1;
if(x==1&&y==n) return 1;
if(x==m&&y==n) return 1;
if(x==m&&y==1) return 1;
return 0;
}
int bfs(int x,int y,int a,int b){
queue<node>q;
memset(vis,0,sizeof(vis));
q.push(node{x,y});
vis[x][y]=1;
while(!q.empty() ){
node f;
f=q.front() ;
q.pop() ;
if(f.x ==a&&f.y ==b){
return false;
}
for(int i=0;i<4;i++){
int dx,dy;
dx=f.x +to[i][0];
dy=f.y +to[i][1];
if(check(dx,dy)&&vis[dx][dy]==0){
q.push(node{dx,dy});
vis[dx][dy]=1;
if(dx==a&&dy==b) {
return false;
}
}
}
}
return true;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
memset(vis,0,sizeof(vis));
int i,j;
scanf("%d%d",&m,&n);
for(i=1;i<=m;i++){
for(j=1;j<=n;j++){
scanf("%d",&map[i][j]);
if(map[i][j]==4){
rx=i;
ry=j;
}
else if(map[i][j]==2) {
sx=i;
sy=j;
}else if(map[i][j]==3){
tx=i;
ty=j;
}
}
}
if(bfs(rx,ry,sx,sy)||bfs(sx,sy,tx,ty)||jiaoluo(sx,sy)){
puts("-1");
continue;
}
queue<node>k;
k.push(node{sx,sy});
memset(vis,-1,sizeof(vis));
vis[sx][sy]=0;
while(!k.empty() ){
node l;
l=k.front() ;
k.pop() ;
if(vis[tx][ty]!=-1){
printf("%d\n",vis[tx][ty]);
break;
}
for(i=0;i<4;i++){
int dx,dy;
dx=l.x +to[i][0];
dy=l.y +to[i][1];
if(!check(l.x -to[i][0],l.y -to[i][1])||map[l.x -to[i][0]][l.y -to[i][1]]==1)continue;
if(check(dx,dy)&&vis[dx][dy]==-1){
k.push(node{dx,dy});
vis[dx][dy]=vis[l.x ][l.y ]+1;
}
}
}
}
}