Problem descriptionthis is a simple game. The goal of the game is to roll two bils 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 bils at the same time. up, down, left, right --- once one of these keys is pressed, bils exist roll to that direction, for example, you pressed up, two bils both roll up.
A ball will stay where it is if its next point is a wall, and ballcan't be overlap.
Your code shoshould give the minimun times you press the keys to achieve the goal.
Inputfirst 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 bils (B) and two holes (h) in a map.
The boundary of the map is always Wils (*).
Outputthe 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
46 3****B**B**H**H****4 4*****BB**HH*****4 4*****BH**HB*****5 6*******.BB***.H*H**..*.*******
Sample output
312Sorry , sir , my poor program fails to get an answer.
Authormadfrog
Sourcehdoj monthly contest-2010.02.06
Idea: the common BFS is a bit difficult to handle when the ball moves. We need to consider the situation where one ball is left and two balls are left.
#include <stdio.h>#include <string.h>struct S{int x[2],y[2],hx[2],hy[2],step,num;}que[1000000],t;int n,m;char mp[25][25];bool vis[25][25][25][25];int main(){ int T,i,j,bf,hf; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); for(i=1;i<=n;i++) scanf("%s",mp[i]+1); bf=hf=0; for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { if(mp[i][j]=='B') { que[0].x[bf]=i; que[0].y[bf]=j; bf++; mp[i][j]='.'; } if(mp[i][j]=='H') { que[0].hx[hf]=i; que[0].hy[hf]=j; hf++; mp[i][j]='.'; } } } int top=0; int bottom=1; que[0].step=0; que[0].num=2; memset(vis,0,sizeof vis); while(top<bottom) { t=que[top]; if(!t.num) { printf("%d\n",t.step); break; } if(t.num==2) { if(t.x[0]<t.x[1])//x-1 { if(t.x[0]-1>=1 && mp[t.x[0]-1][t.y[0]]!='*') t.x[0]--; for(i=0;i<2;i++) if(t.hx[i]==t.x[0] && t.hy[i]==t.y[0]) { t.hx[i]=t.x[0]=0; t.num--; } if(t.x[1]-1>=1 &&mp[t.x[1]-1][t.y[1]]!='*' && !(t.x[1]-1==t.x[0] && t.y[1]==t.y[0])) t.x[1]--; for(i=0;i<2;i++) if(t.hx[i]==t.x[1] && t.hy[i]==t.y[1]) { t.hx[i]=t.x[1]=0; t.num--; } if(!vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]) { vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]=1; if(t.num==1 && !t.x[0]) { t.x[0]=t.x[1]; t.y[0]=t.y[1]; t.x[1]=0; } t.step++; que[bottom++]=t; } } else { if(t.x[1]-1>=1 && mp[t.x[1]-1][t.y[1]]!='*') t.x[1]--; for(i=0;i<2;i++) if(t.hx[i]==t.x[1] && t.hy[i]==t.y[1]) { t.hx[i]=t.x[1]=0; t.num--; } if(t.x[0]-1>=1 &&mp[t.x[0]-1][t.y[0]]!='*' && !(t.x[1]==t.x[0]-1 && t.y[1]==t.y[0])) t.x[0]--; for(i=0;i<2;i++) if(t.hx[i]==t.x[0] && t.hy[i]==t.y[0]) { t.hx[i]=t.x[0]=0; t.num--; } if(!vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]) { vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]=1; if(t.num==1 && !t.x[0]) { t.x[0]=t.x[1]; t.y[0]=t.y[1]; t.x[1]=0; } t.step++; que[bottom++]=t; } }//x-1 t=que[top]; if(t.x[0]>t.x[1])//x+1 { if(t.x[0]+1<=n && mp[t.x[0]+1][t.y[0]]!='*') t.x[0]++; for(i=0;i<2;i++) if(t.hx[i]==t.x[0] && t.hy[i]==t.y[0]) { t.hx[i]=t.x[0]=0; t.num--; } if(t.x[1]+1<=n && mp[t.x[1]+1][t.y[1]]!='*' && !(t.x[1]+1==t.x[0] && t.y[1]==t.y[0])) t.x[1]++; for(i=0;i<2;i++) if(t.hx[i]==t.x[1] && t.hy[i]==t.y[1]) { t.hx[i]=t.x[1]=0; t.num--; } if(!vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]) { vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]=1; if(t.num==1 && !t.x[0]) { t.x[0]=t.x[1]; t.y[0]=t.y[1]; t.x[1]=0; } t.step++; que[bottom++]=t; } } else { if(t.x[1]+1<=n && mp[t.x[1]+1][t.y[1]]!='*') t.x[1]++; for(i=0;i<2;i++) if(t.hx[i]==t.x[1] && t.hy[i]==t.y[1]) { t.hx[i]=t.x[1]=0; t.num--; } if(t.x[0]+1<=n &&mp[t.x[0]+1][t.y[0]]!='*' && !(t.x[1]==t.x[0]+1 && t.y[1]==t.y[0])) t.x[0]++; for(i=0;i<2;i++) if(t.hx[i]==t.x[0] && t.hy[i]==t.y[0]) { t.hx[i]=t.x[0]=0; t.num--; } if(!vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]) { vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]=1; if(t.num==1 && !t.x[0]) { t.x[0]=t.x[1]; t.y[0]=t.y[1]; t.x[1]=0; } t.step++; que[bottom++]=t; } }//x+1 t=que[top]; if(t.y[0]>t.y[1])//y+1 { if(t.y[0]+1<=m && mp[t.x[0]][t.y[0]+1]!='*') t.y[0]++; for(i=0;i<2;i++) if(t.hx[i]==t.x[0] && t.hy[i]==t.y[0]) { t.hx[i]=t.x[0]=0; t.num--; } if(t.y[1]+1<=m && mp[t.x[1]][t.y[1]+1]!='*' && !(t.x[1]==t.x[0] && t.y[1]+1==t.y[0])) t.y[1]++; for(i=0;i<2;i++) if(t.hx[i]==t.x[1] && t.hy[i]==t.y[1]) { t.hx[i]=t.x[1]=0; t.num--; } if(!vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]) { vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]=1; if(t.num==1 && !t.x[0]) { t.x[0]=t.x[1]; t.y[0]=t.y[1]; t.x[1]=0; } t.step++; que[bottom++]=t; } } else { if(t.y[1]+1<=m && mp[t.x[1]][t.y[1]+1]!='*') t.y[1]++; for(i=0;i<2;i++) if(t.hx[i]==t.x[1] && t.hy[i]==t.y[1]) { t.hx[i]=t.x[1]=0; t.num--; } if(t.y[0]+1<=m &&mp[t.x[0]][t.y[0]+1]!='*' && !(t.x[1]==t.x[0] && t.y[1]==t.y[0]+1)) t.y[0]++; for(i=0;i<2;i++) if(t.hx[i]==t.x[0] && t.hy[i]==t.y[0]) { t.hx[i]=t.x[0]=0; t.num--; } if(!vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]) { vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]=1; if(t.num==1 && !t.x[0]) { t.x[0]=t.x[1]; t.y[0]=t.y[1]; t.x[1]=0; } t.step++; que[bottom++]=t; } }//y+1 t=que[top]; if(t.y[0]<t.y[1])//y-1 { if(t.y[0]-1>=1 && mp[t.x[0]][t.y[0]-1]!='*') t.y[0]--; for(i=0;i<2;i++) if(t.hx[i]==t.x[0] && t.hy[i]==t.y[0]) { t.hx[i]=t.x[0]=0; t.num--; } if(t.y[1]-1>=1 && mp[t.x[1]][t.y[1]-1]!='*' && !(t.x[1]==t.x[0] && t.y[1]-1==t.y[0])) t.y[1]--; for(i=0;i<2;i++) if(t.hx[i]==t.x[1] && t.hy[i]==t.y[1]) { t.hx[i]=t.x[1]=0; t.num--; } if(!vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]) { vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]=1; if(t.num==1 && !t.x[0]) { t.x[0]=t.x[1]; t.y[0]=t.y[1]; t.x[1]=0; } t.step++; que[bottom++]=t; } } else { if(t.y[1]-1>=1 && mp[t.x[1]][t.y[1]-1]!='*') t.y[1]--; for(i=0;i<2;i++) if(t.hx[i]==t.x[1] && t.hy[i]==t.y[1]) { t.hx[i]=t.x[1]=0; t.num--; } if(t.y[0]-1>=1 &&mp[t.x[0]][t.y[0]-1]!='*' && !(t.x[1]==t.x[0] && t.y[1]==t.y[0]-1)) t.y[0]--; for(i=0;i<2;i++) if(t.hx[i]==t.x[0] && t.hy[i]==t.y[0]) { t.hx[i]=t.x[0]=0; t.num--; } if(!vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]) { vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]=1; if(t.num==1 && !t.x[0]) { t.x[0]=t.x[1]; t.y[0]=t.y[1]; t.x[1]=0; } t.step++; que[bottom++]=t; } }//y-1 t=que[top]; } else//num==1 { if(t.x[0]+1<=n && mp[t.x[0]+1][t.y[0]]!='*') t.x[0]++; for(i=0;i<2;i++) if(t.hx[i]==t.x[0] && t.hy[i]==t.y[0]) { t.hx[i]=t.x[0]=0; t.num--; } if(!vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]) { vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]=1; t.step++; que[bottom++]=t; } t=que[top]; if(t.x[0]-1>=1 && mp[t.x[0]-1][t.y[0]]!='*') t.x[0]--; for(i=0;i<2;i++) if(t.hx[i]==t.x[0] && t.hy[i]==t.y[0]) { t.hx[i]=t.x[0]=0; t.num--; } if(!vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]) { vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]=1; t.step++; que[bottom++]=t; } t=que[top]; if(t.y[0]+1<=m && mp[t.x[0]][t.y[0]+1]!='*') t.y[0]++; for(i=0;i<2;i++) if(t.hx[i]==t.x[0] && t.hy[i]==t.y[0]) { t.hx[i]=t.x[0]=0; t.num--; } if(!vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]) { vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]=1; t.step++; que[bottom++]=t; } t=que[top]; if(t.y[0]-1>=1 && mp[t.x[0]][t.y[0]-1]!='*') t.y[0]--; for(i=0;i<2;i++) if(t.hx[i]==t.x[0] && t.hy[i]==t.y[0]) { t.hx[i]=t.x[0]=0; t.num--; } if(!vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]) { vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]=1; t.step++; que[bottom++]=t; } } top++; } if(top==bottom) printf("Sorry , sir , my poor program fails to get an answer.\n"); }}