HDU 3309 Roll The Cube题解
Roll The Cube
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 273 Accepted Submission(s): 110
Problem Description
This is a simple game.The goal of the game is to roll two balls to two holes each. 'B' -- ball 'H' -- hole '.' -- land '*' -- wall Remember when a ball rolls into a hole, they(the ball and the hole) disappeared, that is , 'H' + 'B' = '.'. Now you are controlling two balls at the same time.Up, down , left , right --- once one of these keys is pressed, balls exist roll to that direction, for example , you pressed up , two balls both roll up. A ball will stay where it is if its next point is a wall, and balls can't be overlap. Your code should give the minimun times you press the keys to achieve the goal.
Input
First there's an integer T(T<=100) indicating the case number. Then T blocks , each block has two integers n , m (n , m <= 22) indicating size of the map. Then n lines each with m characters. There'll always be two balls(B) and two holes(H) in a map. The boundary of the map is always walls(*).
Output
The minimum times you press to achieve the goal. Tell me "Sorry , sir , my poor program fails to get an answer." if you can never achieve the goal.
Sample Input
4
6 3
***
*B*
*B*
*H*
*H*
***
4 4
****
*BB*
*HH*
****
4 4
****
*BH*
*HB*
****
5 6
******
*.BB**
*.H*H*
*..*.*
******
Sample Output
3
1
2
Sorry , sir , my poor program fails to get an answer.
本题是一般的BFS,只要记着当两个球重合时,只要一个球在洞里,那么另一个求解可以从这里经过,还有就是两球是同时运动的
View Code
1 #include<iostream> 2 using namespace std; 3 #include<queue> 4 #include<cstdio> 5 int dir[4][2]={1,0,0,-1,-1,0,0,1}; 6 struct node 7 { 8 int x[2],y[2]; 9 int step; 10 node(){}; 11 node(int xx1,int yy1,int xx2,int yy2,int vv){x[0]=xx1;y[0]=yy1;x[1]=xx2;y[1]=yy2;step=vv;} 12 }; 13 14 node st; 15 int n,m,cnt=0; 16 17 18 int use[22][22][22][22]={0}; 19 char map[22][22]; 20 inline bool check(int x,int y) 21 { 22 if(x>=0&&x<n&&y>=0&&y<m)return 1; 23 return 0; 24 } 25 26 27 inline bool equal(int x1,int y1,int x2,int y2) 28 { 29 return (x1==x2&&y1==y2); 30 } 31 32 int BFS() 33 { 34 queue<node> q; 35 q.push(st); 36 node cur; 37 while(!q.empty()) 38 { 39 cur=q.front(); 40 q.pop(); 41 int x1,y1,x2,y2; 42 int flag=0; 43 for(int i=0;i<4;i++) 44 { 45 if(map[cur.x[0]][cur.y[0]]=='H') 46 { 47 x1=cur.x[0]; 48 y1=cur.y[0]; 49 flag=1; 50 } 51 else 52 { 53 x1=cur.x[0]+dir[i][0]; 54 y1=cur.y[0]+dir[i][1]; 55 } 56 if(check(x1,y1)==0||map[x1][y1]=='*') 57 { 58 x1=cur.x[0]; 59 y1=cur.y[0]; 60 } 61 62 if(map[cur.x[1]][cur.y[1]]=='H'&&!flag) 63 { 64 x2=cur.x[1]; 65 y2=cur.y[1]; 66 } 67 else 68 { 69 x2=cur.x[1]+dir[i][0]; 70 y2=cur.y[1]+dir[i][1]; 71 } 72 73 74 if(check(x2,y2)==0||map[x2][y2]=='*') 75 { 76 x2=cur.x[1]; 77 y2=cur.y[1]; 78 } 79 80 if(use[x1][y1][x2][y2]==cnt) continue; 81 if(equal(x1,y1,x2,y2)) 82 { 83 if(map[x1][y1]=='H'||map[x2][y2]=='H') 84 { 85 use[x1][y1][x2][y2]=cnt; 86 use[x2][y2][x1][y1]=cnt; 87 q.push(node(x1,y1,x2,y2,cur.step+1)); 88 } 89 } 90 else 91 { 92 if(map[x1][y1]=='H'&&map[x2][y2]=='H')return cur.step+1; 93 use[x1][y1][x2][y2]=cnt; 94 use[x2][y2][x1][y1]=cnt; 95 q.push(node(x1,y1,x2,y2,cur.step+1)); 96 } 97 } 98 } 99 return -1; 100 } 101 102 int main() 103 { 104 int kase,k; 105 scanf("%d",&kase); 106 while(kase--) 107 { 108 scanf("%d%d",&n,&m); 109 cnt++; 110 for(int i=0;i<n;i++) 111 scanf("%s",map[i]); 112 k=0; 113 for(int i=0;i<n&&k<2;i++) 114 for(int j=0;j<m&&k<2;j++) 115 { 116 if(map[i][j]=='B') 117 { 118 st.x[k]=i; 119 st.y[k]=j; 120 map[i][j]='.'; 121 k++; 122 if(k==2) 123 break; 124 } 125 } 126 127 st.step=0; 128 use[st.x[0]][st.y[0]][st.x[1]][st.y[1]]=cnt; 129 use[st.x[1]][st.y[1]][st.x[0]][st.y[0]]=cnt; 130 int ans=BFS(); 131 if(ans==-1)printf("Sorry , sir , my poor program fails to get an answer.\n"); 132 else printf("%d\n",ans); 133 } 134 return 0; 135 }


浙公网安备 33010602011771号