HDU-3309-Roll The Cube(BFS)
来源:互联网 发布:福建游龙网络 编辑:程序博客网 时间:2024/05/16 11:16
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.
'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(*).
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.
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.
Author
MadFroG
Source
HDOJ Monthly Contest – 2010.02.06
思路:普通的BFS,就是球移动的时候处理有点麻烦,要分还剩一个球和还剩两个球的情况考虑。
#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"); }}
54 0
- hdu 3309 Roll The Cube ( bfs )
- HDU-3309-Roll The Cube(BFS)
- HDU 3309 Roll The Cube (BFS)
- hdu 3309 Roll The Cube(bfs)
- hdu 3309 Roll The Cube【BFS】
- HDU 3309 Roll The Cube(bfs)
- HDU 3309 Roll The Cube
- HDOJ 3309 Roll The Cube (BFS)
- HDU3309:Roll The Cube(BFS)
- hdu3309 Roll The Cube (bfs)
- hdu 3309 Roll The Cube 广搜
- HDOJ题目3309 Roll The Cube(BFS)
- HDU3309-Roll The Cube
- Guessing the Dice Roll HDU
- HDU3309Roll The Cube(BFS)
- HDU 4801 Pocket Cube BFS
- HDU 4801 Pocket Cube bfs
- hdu 5955 Guessing the Dice Roll
- poj 3468 A Simple Problem with Integers(线段树的区间更新与求和)
- Android-Adapter
- C++多态的实现原理
- Eclipse设置dtd对struts.xml语法提示
- 王家林最受欢迎的一站式云计算大数据和移动互联网解决方案课程 V1(20140809)之云计算大数据下MySQL企业级开发最佳实践
- HDU-3309-Roll The Cube(BFS)
- 2014.08.09 CH Round#48 - Streaming#3(NOIP模拟赛Day1) 总结
- 读懂diff
- Word Break II
- 8.9JZOJNOIP模拟
- 转载_CI 系统搭建:Git、Gerrit与Jenkins
- jzoj NOIP2014提高组模拟8.9总结
- xml 备份短信 序列化器 XmlSerializer
- 佘祥林案