码迷,mamicode.com
首页 > 其他好文 > 详细

(bfs) hdu 1254

时间:2015-03-21 11:12:54      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

推箱子

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


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
 

 

Author
Ignatius.L & weigang Lee
 

 

Recommend
Ignatius.L   |   We have carefully selected several similar problems for you:  1253 1026 1175 1072 1240 
 
 
比较神奇。。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
int tt,n,m,map[10][10];
bool vis[10][10][10][10];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
struct node
{
    int x,y,bx,by,step;
    friend bool operator<(node a,node b)
    {
        return a.step>b.step;
    }
}star;
bool check(int x,int y)
{
    if(x<0||y<0||x>=n||y>=m||map[x][y]==1)
        return false;
    return true;
}
void input()
{
    star.step=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            scanf("%d",&map[i][j]);
            if(map[i][j]==2)
            {
                star.bx=i,star.by=j;
                map[i][j]=0;
            }
            else if(map[i][j]==4)
            {
                star.x=i,star.y=j;
                map[i][j]=0;
            }
        }
    }
}
int bfs()
{
    int xx,yy,x,y;
    node now,next;
    memset(vis,0,sizeof(vis));
    vis[star.x][star.y][star.bx][star.by]=1;
    priority_queue<node> q;
    q.push(star);
    while(!q.empty())
    {
        now=q.top(),q.pop();
        if(map[now.bx][now.by]==3)
        {
            return now.step;
        }
        for(int i=0;i<4;i++)
        {
            xx=now.x+dx[i],yy=now.y+dy[i];
            if(!check(xx,yy)) continue;
            next.bx=now.bx;
            next.by=now.by;
            next.step=now.step;
            next.x=xx;
            next.y=yy;
            if(xx==now.bx&&yy==now.by)
            {
                x=xx+dx[i],y=yy+dy[i];
                if(!check(x,y)) continue;
                next.bx=x,next.by=y;
                next.step++;
            }
            if(vis[xx][yy][next.bx][next.by])
                continue;
            vis[xx][yy][next.bx][next.by]=1;
            q.push(next);
        }
    }
    return -1;
}
int main()
{
    scanf("%d",&tt);
    while(tt--)
    {
        scanf("%d%d",&n,&m);
        input();
        printf("%d\n",bfs());
    }
    return 0;
}

  

(bfs) hdu 1254

标签:

原文地址:http://www.cnblogs.com/a972290869/p/4355251.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!