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
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值