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<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;
}
- Push Box
- Push Box
- HDU 1732 push Box
- HDU Push Box
- HDU 1732 Push Box
- hdu1732 Push Box (bfs)
- hdu1732 Push Box ---- BFS
- hdu1732 Push Box【搜索】
- Push Box (BFS)
- Push Box(推箱子)
- HDU 1732 Push Box(搜索)
- 杭电 1732 Push Box
- hdu 1732 Push Box(BFS)
- HDU---1732 Push Box【BFS】
- bfs hdu 1732 push the box
- HDU 1732 Push Box(搜索)
- HDU-1732 Push Box BFS + BFS
- hdu 1732 Push Box(bfs)
- (转载)如何成为一名优秀的程序员?
- GCD 串行队列,并发队列和主队列的整理
- 红帽企业版yum时提示系统未注册的解决办法
- 最精确的手机电量校正方法,完全恢复电量,提高使用时间
- Effective C++ 学记之16 成对使用new和delete时要采取相同形式
- Push Box
- IPHONE实景导航开发总结
- 使用 Counter Analyzer 进行 Performance Monitor Counter 数据分析
- [小代码]在Android和PHP之间的加密/解密,AES加密
- 点类为基类声明为共有继承调用基类的私有成员
- 五月第二周 题解 交大OJ
- Clairewd’s message +KMP
- 自己搭建家庭“云”
- topcoder 2C 1000 WellTimedSearch dfs+枚举