hdu 1732 Push Box(BFS)

Push Box

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)
Total Submission(s): 1307    Accepted Submission(s): 505


Problem Description
Push Box is a classic puzzle game. This game play in a grid, there are five types of block in it, the player, the box, the hole, empty place, and the wall. In every step, player can move up, down, left, or right, if the target place is empty. Moreover, if a box in the target place, and the next place in that direction is empty, player can move to the target place, and then push the box to the next place. Remember, both of the player and boxes can't move out of the grid, or you may assume that there is a wall suround the whole grid. The objective of this game is to push every box to a hole. Now, your problem is to find the strategy to achieve the goal with shortest steps, supposed there are exactly three boxes.
 

Input
The input consists of several test cases. Each test case start with a line containing two number, n, m(1 < n, m ≤ 8), the rows and the columns of grid. Then n lines follow, each contain exact m characters, representing the type of block in it. (for empty place, X for player, * for box, # for wall, @ for hole). Each case contain exactly one X, three *, and three @. The input end with EOF.
 

Output
You have to print the length of shortest strategy in a single line for each case. (-1 if no such strategy)
 

Sample Input
  
  
4 4 .... ..*@ ..*@ .X*@ 6 6 ...#@. @..*.. #*##.. ..##*# ..X... .@#...
 

Sample Output
  
  
7 11
 

题意:跟推箱子游戏的规则一样,不过是一个人推3个箱子到三个洞,求最少人要走几步,注意是人的步数不是推的次数

思路:跟上个题不一样,应该说比上个题还简单一点,这个题要求的是人的步数,那么我们只要对人做BFS即可

注意箱子有可能推入洞后还要推出来

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#define N 9
int n,m;
char ma[N][N];
bool vis[N][N][N][N][N][N][N][N];///需要三个箱子和人位置确定才能确定状态
int dir[4][2]= {-1,0,1,0,0,-1,0,1};
struct Node
{
    int step;
    int x[4],y[4];
};
struct Node1
{
    int step;
    int x,y;
};
int check(int x,int y)
{
    if(x<0||x>=n||y<0||y>=m||ma[x][y]=='#') return 0;
    return 1;
}
int bfs()
{
    int cnt=0;
    Node a,next;
    for(int i=0; i<n; i++)
        for(int j=0; j<m; j++)
        {
            if(ma[i][j]=='X')
                a.x[3]=i,a.y[3]=j;
            else if(ma[i][j]=='*')
                a.x[cnt]=i,a.y[cnt++]=j;
        }
    a.step=0;
    queue<Node>que;
    que.push(a);
    memset(vis,false,sizeof(vis));
    vis[a.x[0]][a.y[0]][a.x[1]][a.y[1]][a.x[2]][a.y[2]][a.x[3]][a.y[3]]=true;
    while(!que.empty())
    {
        Node now=que.front();
        que.pop();
        if(ma[now.x[0]][now.y[0]]=='@'&&ma[now.x[1]][now.y[1]]=='@'&&ma[now.x[2]][now.y[2]]=='@')
            return now.step;
        for(int i=0; i<4; i++)
        {
            next=now;
            next.x[3]+=dir[i][0],next.y[3]+=dir[i][1];
            if(!check(next.x[3],next.y[3])) continue;
            int flag=0;
            for(int j=0; j<3; j++)
                if(next.x[3]==next.x[j]&&next.y[3]==next.y[j])
                {
                    next.x[j]+=dir[i][0],next.y[j]+=dir[i][1];
                    if(!check(next.x[j],next.y[j]))
                        flag=1;
                    for(int k=0;k<3;k++)
                        if(j!=k&&next.x[j]==next.x[k]&&next.y[j]==next.y[k])
                            flag=1;
                }
            if(flag) continue;
            if(vis[next.x[0]][next.y[0]][next.x[1]][next.y[1]][next.x[2]][next.y[2]][next.x[3]][next.y[3]]) continue;
            vis[next.x[0]][next.y[0]][next.x[1]][next.y[1]][next.x[2]][next.y[2]][next.x[3]][next.y[3]]=1;
            next.step=now.step+1;
            que.push(next);
        }
    }
    return -1;
}
int main()
{
    while(~scanf("%d %d",&n,&m))
    {
        for(int i=0; i<n; i++)
            scanf("%s",ma[i]);
        printf("%d\n",bfs());
    }
    return 0;
}






  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值