hdu 1732 Push Box(bfs)

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

题意:类似推箱子游戏,三个箱子,三个目的地,求出人把三个箱子都推到目的地的最小步数,若没法完成就输出-1。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <queue>
#define INF 100000000
using namespace std;
int n,m;
char map[9][9];
bool vis[8][8][8][8][8][8][8][8];
bool aim[8][8];
int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
struct point
{
    int x;
    int y;

};
struct node
{
    point p[4];
    int step;
};
node s,e;
bool ok(node &a)  //箱子是否全部进洞
{
    for (int i=1; i<=3; i++)
    {
        if(!aim[a.p[i].x][a.p[i].y])
            return false;
    }
    return true;
}
int bfs()
{
    memset(vis, false, sizeof(vis));
    memset(aim, false, sizeof(aim));
    for (int i=0; i<3; i++)
        aim[e.p[i].x][e.p[i].y]=true;  //目标状态
    
    queue<node>q;
    node temp;
    q.push(s);
    vis[s.p[0].x][s.p[0].y][s.p[1].x][s.p[1].y][s.p[2].x][s.p[2].y][s.p[3].x][s.p[3].y]=true;
    while (!q.empty())
    {
        s=q.front();
        q.pop();
        if(ok(s))
            return s.step;
        for (int i=0; i<4; i++)
        {
            temp=s;
            temp.p[0].x+=dir[i][0]; //人移动
            temp.p[0].y+=dir[i][1];
            int x=temp.p[0].x;
            int y=temp.p[0].y;
            if(x<0 || x>=n || y<0 || y>=m || map[x][y]=='#')
                continue;
            int j;
            //判断人是否遇到箱子
            for (j=1; j<=3; j++)
            {
                if(x==temp.p[j].x && y==temp.p[j].y)
                {
                    break;
                }
            }
            if(j==4) //人未遇到箱子
            {
                if(!vis[x][y][temp.p[1].x][temp.p[1].y][temp.p[2].x][temp.p[2].y][temp.p[3].x][temp.p[3].y])
                {
                    temp.step++;
                    vis[x][y][temp.p[1].x][temp.p[1].y][temp.p[2].x][temp.p[2].y][temp.p[3].x][temp.p[3].y]=true;
                    q.push(temp);
                }
            }
            else //遇到箱子,则移动箱子
            {
                //箱子移动后的坐标
                x+=dir[i][0];
                y+=dir[i][1];
                if(x<0 || x>=n || y<0 || y>=m || map[x][y]=='#')
                    continue;
                int k;
                for (k=1; k<=3; k++) //判断是否有其它箱子阻挡
                {
                    if(x==temp.p[k].x && y==temp.p[k].y)
                        break;
                }
                if(k==4) //没有其它箱子阻挡
                {
                    temp.p[j].x=x;
                    temp.p[j].y=y;
                    if(!vis[temp.p[0].x][temp.p[0].y][temp.p[1].x][temp.p[1].y][temp.p[2].x][temp.p[2].y][temp.p[3].x][temp.p[3].y])
                    {
                        temp.step++;
                        vis[temp.p[0].x][temp.p[0].y][temp.p[1].x][temp.p[1].y][temp.p[2].x][temp.p[2].y][temp.p[3].x][temp.p[3].y]=true;
                        q.push(temp);
                    }
                }
            }
        }
    }
    return -1;
}
int main()
{
    int cnt,cnt2;
    while (scanf("%d%d",&n,&m)!=EOF)
    {
        cnt=0;
        cnt2=0;
        for (int i=0; i<n; i++)
        {
            scanf("%s",map[i]);
            for (int j=0; j<m; j++)
            {
                if(map[i][j]=='X')
                {
                    s.p[0].x=i;
                    s.p[0].y=j;
                }
                else if(map[i][j]=='*')
                {
                    s.p[++cnt].x=i;
                    s.p[cnt].y=j;
                }
                else if(map[i][j]=='@')
                {
                    e.p[cnt2].x=i;
                    e.p[cnt2++].y=j;
                }
                if(map[i][j]!='#')
                    map[i][j]='.';
            }
        }
        s.step=0;
        printf("%d\n",bfs());
    }
    return 0;
}


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值