Push Box

来源:互联网 发布:unity3d和kinect 编辑:程序博客网 时间:2024/05/22 01:27

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
711
 


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;
}


原创粉丝点击