Hdoj title 3309 Roll The Cube (BFS)

Source: Internet
Author: User

Roll the CubeTime limit:3000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 502 Accepted Submission (s): 181


Problem DescriptionThis is a simple game. The goal of the game is to roll from balls to both holes each.
' B '--ball
' H '--hole
'. '--Land
' * '--wall
Remember when a ball rolls to a hole, they (the ball and the hole) disappeared, which is, ' H ' + ' B ' = '. '.
Now is controlling and balls at the same time. Up, down, left, right---once one of these keys are pressed, balls exist roll to that direction, for example, you press Ed up, and the balls both roll up.
A Ball would stay where it is if the next point was a wall, and balls can ' t be overlap.
Your code should 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 a integers n, m (n, m <=) indicating size of the map.
Then n lines each with M characters.
There ' ll always be balls (B) and both holes (H) in a map.
The boundary of the map is always walls (*).
Outputthe minimum times you press to achieve the goal.
Tell me "Sorry, sir, my poor program fails to get a answer." If you can never achieve the goal.
Sample Input
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 a answer.

Authormadfrog
Sourcehdoj Monthly contest–2010.02.06
RECOMMENDWXL | We have carefully selected several similar problems for you:3308 3314 3307 3306 3310 AC Code
#include <stdio.h> #include <string.h> #include <iostream> #include <queue>using namespace std; int N,m,vis[25][25][25][25],sx[2],sy[2];char map[25][25];int dx[4]={0,1,0,-1};int dy[4]={1,0,-1,0};struct s{int x[2] , y[2],step,b[2],h[2];//friend bool operator < (s a,s b)//{//return a.step>b.step;//}}a,temp;int BFS () {memset (Vis , 0,sizeof (Vis)); a.x[0]=sx[0],a.x[1]=sx[1];a.y[0]=sy[0],a.y[1]=sy[1];a.b[0]=a.b[1]=a.h[0]=a.h[1]=0;vis[sx[0]][ Sy[0]][sx[1]][sy[1]]=1;a.step=0;//priority_queue<struct s>q;queue<struct S>q;q.push (a); while (! Q.empty ()) {int i,j;//a=q.top (); A=q.front (); Q.pop (); for (i=0;i<4;i++) {temp=a;for (j=0;j<2;j++) {if (Temp.b[j]) Continue;temp.x[j]=a.x[j]+dx[i];temp.y[j]=a.y[j]+dy[i];if (map[temp.x[j]][temp.y[j]]== ' * ') {temp.x[j]=a.x[j]; TEMP.Y[J]=A.Y[J];}} if (Vis[temp.x[0]][temp.y[0]][temp.x[1]][temp.y[1]]) continue;if (temp.x[0]==temp.x[1]&&temp.y[0]==temp.y [1]&&temp.b[0]+temp.b[1]==0] Continue;vis[temp.x[0]][temp.y[0]][temp.x[1]][temp.y[1]]=1;temp.step=a.step+1;int flag=1;for (j=0;j<2;j++) {int now=map[temp.x[j]][temp.y[j]];if (now<2&&! Temp.h[now]) {temp.h[now]=1;temp.b[j]=1;} if (!temp.b[j]) flag=0;} if (flag) return Temp.step;q.push (temp);}} return-1;} int main () {int t;scanf ("%d", &t), while (t--) {int i,j;scanf ("%d%d", &n,&m); int Cnt=0,cot=0;for (i=0;i<n; i++) {scanf ("%s", Map[i]), for (j=0;j<m;j++) {if (map[i][j]== ' H ') {map[i][j]=cnt++;} if (map[i][j]== ' B ') {sx[cot]=i;sy[cot]=j;cot++;}}} int Ans=bfs (); if (ans==-1) {printf ("Sorry, sir, my poor program fails to get an answer.\n");} elseprintf ("%d\n", ans);}}


Hdoj title 3309 Roll The Cube (BFS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.