Push Box

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
 


Source
“网新恩普杯”杭州电子科技大学程序设计邀请赛
 

Recommend
lcy
 
#include<iostream>
#include<queue>
using namespace std;
typedef struct point{
  int x,y;
}Point;
int N,M;
char map[9][9];
typedef struct node{
  Point p,box[3];
  
 int count;
 bool isok()
 {
   if(p.x>=0&&p.x<N&&p.y>=0&&p.y<M&&map[p.x][p.y]!='#')
   {
     return true;
   }
   return false;
 }
}Node;






int direction[][2]={{1,0},{-1,0},{0,1},{0,-1}};
bool isvisit[9][9][9][9][9][9][9][9];




int  isbox(Node n)
{
   for(int i=0;i<3;i++)
   {
  if(n.p.x==n.box[i].x&&n.p.y==n.box[i].y)
  {
    return i;
  }
      
   }
  
   return -1;
}
bool canmove(Node n,int dir,int which)
{
n.p.x+=direction[dir][0];
n.p.y+=direction[dir][1];
if(!n.isok())
return false;
     for(int i=0;i<3;i++)
{
if(i!=which&&n.p.x==n.box[i].x&&n.p.y==n.box[i].y)
return false;
}
 
return true;
  
}
bool getisvisit(Node& n)
{
return isvisit[n.p.x][n.p.y][n.box[0].x][n.box[0].y][n.box[1].x][n.box[1].y][n.box[2].x][n.box[2].y];
   
}
void setisvisit(Node& n)
{
    isvisit[n.p.x][n.p.y][n.box[0].x][n.box[0].y][n.box[1].x][n.box[1].y][n.box[2].x][n.box[2].y]=true;
}


bool finish(Node n)
{
   if(map[n.box[0].x][n.box[0].y]=='@'&&map[n.box[1].x][n.box[1].y]=='@'&&map[n.box[2].x][n.box[2].y]=='@')
       return true;
   return false;
}
void bfs(Node start)
{

queue<Node> myqueue;
myqueue.push(start);
setisvisit(start);
while(!myqueue.empty())
{
  Node pre=myqueue.front();
  myqueue.pop();
  if(finish(pre))
  {
    cout<<pre.count<<endl;
return;
  }
  for(int i=0;i<4;i++)
  {
     Node cur=pre;
 cur.p.x+=direction[i][0];
 cur.p.y+=direction[i][1];
 cur.count++;
 if(cur.isok())
 {
    int bi=isbox(cur);
if(bi!=-1)
{
   if(canmove(cur,i,bi))
{
  cur.box[bi].x+=direction[i][0];
  cur.box[bi].y+=direction[i][1];
  if(!getisvisit(cur))
  {
    setisvisit(cur);
myqueue.push(cur);
  }
}
}
else
{
   if(!getisvisit(cur))
  {
    setisvisit(cur);
myqueue.push(cur);
  }
}
 }
            
  
  }

}


   cout<<-1<<endl;
}
int main()
{
// freopen("in.txt","r",stdin);
Node start;
while(scanf("%d%d",&N,&M)!=EOF)
{
int c=0;
  for(int i=0;i<N;i++)
  {
    for(int j=0;j<M;j++)
{
  cin>>map[i][j];
  
  if(map[i][j]=='X')
  {
    start.p.x=i;
start.p.y=j;
 
  }
  if(map[i][j]=='*')
  {
    start.box[c].x=i;
start.box[c].y=j;
c++;
 
  }
}

  }
  start.count=0;
  memset(isvisit,false,sizeof(isvisit));
  bfs(start);


}
return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值