hdu 1254 推箱子游戏

这个题目我就不说,链接在这里http://acm.hdu.edu.cn/showproblem.php?pid=1254

主要我想说的是,一开始是我用2个三围数组来分别标记人和箱子走过的四个方向,但不知怎么回事行不通。。。

于是就用一个四维数组来标记状态,嘻嘻,过了。。。

好吧,还是直接上代码了。。。。

 1 #include<iostream>
 2 #include<cstring>
 3 #include<queue>
 4 
 5 using namespace std;
 6 int visited[8][8][8][8];//记录人和箱子的状态
 7 int map[8][8];
 8 int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//上、下、左、右
 9 int m,n;
10 int px,py,sx,sy,ex,ey;
11 
12 struct point {
13     int x,y; //人的位置
14     int bx,by;  //箱子的位置
15     int step;
16     bool operator <(const point &p) const {
17         return p.step<step;
18     }
19 };
20 
21 void bfs(int x,int y){
22     priority_queue<point>Q;  
23     point s;
24     s.x=x,s.y=y,s.bx=sx,s.by=sy,s.step=0;
25     Q.push(s);
26     while(!Q.empty()){
27         point p;
28         p=Q.top();
29         Q.pop();
30         if(p.bx==ex&&p.by==ey){
31             printf("%d\n",p.step);
32             return ;
33         }
34         for(int i=0;i<4;i++){
35             point q;
36             q.x=p.x+dir[i][0];
37             q.y=p.y+dir[i][1];
38             q.bx=p.bx;
39             q.by=p.by;
40             q.step=p.step;
41             if(q.x<0||q.x>=m||q.y<0||q.y>=n)
42                 continue;
43             if(map[q.x][q.y]==1||visited[q.x][q.y][q.bx][q.by])
44                 continue;
45             //如果找到了箱子,此时要推的方向与人的方向一致
46             if(q.x==q.bx&&q.y==q.by){
47                 q.bx=p.bx+dir[i][0];
48                 q.by=p.by+dir[i][1];
49                 q.step+=1;
50                 if(q.bx<0||q.bx>=m||q.by<0||q.by>=n)
51                     continue;
52                 if(visited[q.x][q.y][q.bx][q.by]||map[q.bx][q.by]==1)
53                     continue;
54                 visited[q.x][q.y][q.bx][q.by]=1;
55             }
56             Q.push(q);
57             visited[q.x][q.y][q.bx][q.by]=1;
58         }
59     }
60     printf("-1\n");
61 }
62     
63 
64 int main(){
65     int t;
66     scanf("%d",&t);
67     while(t--){
68         scanf("%d%d",&m,&n);
69         for(int i=0;i<m;i++){
70             for(int j=0;j<n;j++){
71                 scanf("%d",&map[i][j]);
72                 if(map[i][j]==2){
73                     sx=i,sy=j;
74                 }else if(map[i][j]==3){
75                     ex=i,ey=j;
76                     map[i][j]=0;
77                 }else if(map[i][j]==4){
78                     px=i,py=j;
79                     map[i][j]=0;
80                 }
81             }
82         }
83         memset(visited,0,sizeof(visited));
84         bfs(px,py);
85     }
86     return 0;
87 }
88         

 

posted @ 2013-01-27 14:25  ihge2k  阅读(223)  评论(0编辑  收藏  举报