hdu1254(bfs+bfs)

地址:http://acm.hdu.edu.cn/showproblem.php?pid=1254

推箱子

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4040    Accepted Submission(s): 1106


Problem 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
 

容易忘记的测试数据:

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

箱子走过的路径可以再走一遍。所以记忆箱子走到该点时的方向,按此设为重复。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct node{
	int x,y,t,bx,by;
};
int map[8][8],m,n,a[4][2]={-1,0,1,0,0,-1,0,1};
int vi[8][8][4],yes,vb[8][8];   //vi即为记忆数组,前两个对应点的坐标,最后一个表示到该点时的方向
int find(node s)
{
	if(s.x>=0&&s.y>=0&&s.x<m&&s.y<n&&map[s.x][s.y]!=1)
		return 1;
	else return 0;
}
int findm(node s)
{
	if(s.bx>=0&&s.by>=0&&s.bx<m&&s.by<n&&map[s.bx][s.by]!=1)
		return 1;
	else return 0;
}
void bfsm(node here)
{
	queue<node> qi;
	qi.push(here);
	node next;
	int i,xx,xy;
	xx=here.x;xy=here.y;vb[here.bx][here.by]=1;
	while(!qi.empty())
	{
		here=qi.front();
		qi.pop();
		for(i=0;i<4;i++)
		{
			next.bx=here.bx+a[i][0];
			next.by=here.by+a[i][1];
			if(findm(next)&&(next.bx!=xx||next.by!=xy)&&!vb[next.bx][next.by])
			{
				vb[next.bx][next.by]=1;
				qi.push(next);
			}
		}
	}
}
int bfs(node here)
{
	queue<node> qi;
	qi.push(here);
	node next;
	int i,j;
	while(!qi.empty())
	{
		here=qi.front();
		qi.pop();
		memset(vb,0,sizeof(vb));
		bfsm(here);
		/*for(i=0;i<m;i++)
		{
			for(j=0;j<n;j++)
				printf("%d ",vb[i][j]);
			printf("\n");
		}*/
		for(i=0;i<4;i++)
		{
			next.x=here.x+a[i][0];
			next.y=here.y+a[i][1];
			next.bx=here.x;
			next.by=here.y;
			next.t=here.t+1;
			if(find(next)&&!vi[next.x][next.y][i]&&vb[here.x-a[i][0]][here.y-a[i][1]])
			{
				if(map[next.x][next.y]==3) return next.t;
				vi[next.x][next.y][i]=1;
				qi.push(next);
			}
		}
	}
	return 0;
}
int main()
{
	int t,i,j,ans;
	node xiang;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&m,&n);
		memset(vi,0,sizeof(vi));
		for(i=0;i<m;i++)
		{
			for(j=0;j<n;j++)
			{
				scanf("%d",&map[i][j]);
				if(map[i][j]==2)
				{
					xiang.x=i;
					xiang.y=j;
					xiang.t=0;
				}
				if(map[i][j]==4)
				{
					xiang.bx=i;
					xiang.by=j;
				}
			}
		}
		ans=bfs(xiang);
		if(!ans) printf("-1\n");
		else printf("%d\n",ans);
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值