HDU1254-推箱子

来源:互联网 发布:匹克模考tpo软件 编辑:程序博客网 时间:2024/06/09 13:05

推箱子

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 71   Accepted Submission(s) : 31

Font: Times New Roman | Verdana | Georgia

Font Size:  

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

15 50 3 0 0 01 0 1 4 00 0 1 0 01 0 2 0 00 0 0 0 0

Sample Output

4

Author

Ignatius.L & weigang Lee


解题思路:bfs+dfs,对箱子进行bfs,对人进行dfs


#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <algorithm>#include <queue>#include <vector>#include <set>#include <bitset>#include <stack>#include <map>#include <climits>#include <functional>using namespace std;#define LL long longconst int INF=0x3f3f3f3f;int xx[8]={-1,0,1,0,-1,0,1,0};int yy[8]={0,-1,0,1,0,-1,0,1};bool mp[10][10][5];int a[10][10],b[10][10];int x1,y1,x2,y2,tx,ty,px,py;int N,M;bool flag;struct node{    int x,y,px,py,step;}n,m;bool dfs(int x,int y){    if(x==tx&&y==ty) return true;    if(x<0||x>=N||y<0||y>=M) return false;    if(x==m.x&&y==m.y) return false;    if(b[x][y]==1||a[x][y]==1) return false;    b[x][y]=1;    return (dfs(x+1,y)||dfs(x-1,y)||dfs(x,y+1)||dfs(x,y-1));}int main(){    int t;    scanf("%d", &t);    while(t--)    {        scanf("%d %d", &N, &M);        for(int i = 0; i < N; i++)        {            for(int j = 0; j < M; j++)            {                scanf("%d",&a[i][j]);                b[i][j]=a[i][j];                if(a[i][j]==2) x1=i,y1=j;                if(a[i][j]==3) x2=i,y2=j;                if(a[i][j]==4) px=i,py=j;            }        }        memset(mp,false,sizeof mp);        flag=false;        n.x=x1;n.y=y1;n.step=0;        n.px=px;n.py=py;        queue<node>q;        q.push(n);        while(!q.empty())        {            m=q.front();            q.pop();            if(m.x==x2&&m.y==y2)            {                flag=true;                break;            }            for(int i=0;i<4;i++)            {                n.x=m.x+xx[i];                n.y=m.y+yy[i];                tx=m.x+xx[i+2];                ty=m.y+yy[i+2];                n.step=m.step+1;                if(n.x>=0&&n.x<N&&n.y>=0&&n.y<M&&!mp[n.x][n.y][i]&&a[n.x][n.y]!=1)                {                    memset(b,0,sizeof b);                    if(tx>=0&&tx<N&&ty>=0&&ty<M&&a[tx][ty]!=1&&dfs(m.px,m.py))                    {                        n.px=tx;n.py=ty;                        mp[n.x][n.y][i]=true;                        q.push(n);                    }                }            }        }        if(flag) printf("%d\n",m.step);        else printf("-1\n");    }    return 0;}

0 0