POJ 1475 pushing Boxes push box (double BFS)

Source: Internet
Author: User

Title Link: http://poj.org/problem?id=1475

A set of test data:

7 3

###

. T.

. S.

#B #

...

...

...

Results:

Problem-solving ideas: First to determine whether there is a box around the empty space, if there is, to determine if the person can reach the side of the box, if possible, put the box into the empty place, and then continue

AC Code:

1 //problem-solving ideas: First to determine whether there is a box around the empty space, if there is, to determine if the person can reach the side of the box, if possible, put the box into the empty place, and then continue2#include <iostream>3#include <algorithm>4#include <cstdio>5#include <cstring>6#include <queue>7#include <string>8#include <cmath>9 using namespacestd;Ten intBx,by,sx,sy,tx,ty; One intm,n,dir[][2]={-1,0,1,0,0,-1,0,1};//Note that the order of N, S, W, E is required because this WA was once A Charop[]={'N','s','W','e'}; - BOOLmark[ -][ -][4];//mark the location around the box has been used - intvis[ -][ -];//the place where the marker man walked the Charmap[ -][ -]; - structBb//Box - { -     intX,y,sx,sy;//Sx,sy indicates that the current box is fixed, where the person is located +     stringans; - }bnow,beed; + structYy//people A { at     intx, y; -     stringans; - }ynow,yeed; - CharUpCharc) - { -     return(C-'a'+'A'); in } - //AA,BB represents the position of the current box; Ss,ee represents the starting point; to BOOLBFS2 (intSintEintAaintBbintSsintEe//find out if the current person can reach the location specified in the box; + { -Queue<yy>yy; the     if(s<0|| S>m | | e<0|| E>n | | Map[s][e] = ='#')return false; *ynow.x = SS; YNOW.Y = EE; ynow.ans=""; $memset (Vis,0,sizeof(Vis));Panax NotoginsengVIS[AA][BB] =1;//can't go through the box -Vis[ss][ee] =1;//start Mark as the Yy.push (ynow); +      while(!yy.empty ()) A     { theYnow =Yy.front (); + Yy.pop (); -         if(ynow.x = = S && ynow.y = =e) $         { $             return true; -         } -          for(intI=0;i<4; i++) the         { -Yeed.x = ynow.x+dir[i][0];WuyiYeed.y = ynow.y+dir[i][1]; the             if(yeed.x>0&& yeed.x<=m && yeed.y>0&& yeed.y<=n &&!vis[yeed.x][yeed.y] && map[yeed.x][yeed.y]!='#') -             { WuYeed.ans = Ynow.ans+op[i];//record the path traversed -VIS[YEED.X][YEED.Y] =1; About Yy.push (yeed); $             } -         } -     } -     return false; A } + BOOLBFS1 () the { -Queue<bb>BB; $bnow.x = bx;bnow.y=by;bnow.ans=""; theBnow. SX = Sx;bnow. sy=Sy; the Bb.push (bnow); the     while(!bb.empty ()) the      { -  inbnow=Bb.front (); the Bb.pop (); the        if(bnow.x = = TX && bnow.y==ty) About        { the            return true; the        } the         for(intI=0;i<4; i++)//Four directions around the box; +        { -Beed.x = bnow.x+dir[i][0]; theBeed.y = bnow.y+dir[i][1];Bayi            if(beed.x>0&& beed.x<=m && beed.y>0&& beed.y<=n &&!mark[beed.x][beed.y][i] && map[beed.x][beed.y]!='#') the            { the                if(BFS2 (beed.x-2*dir[i][0],beed.y-2*dir[i][1],bnow.x,bnow.y,bnow. Sx,bnow. SY))//if can be pushed to yeed, then need to judge whether the person can reach, its last point; -                { -Beed. SX = bnow.x;//after pushing the box, the man's position is on the box. theBeed. SY =bnow.y; theBeed.ans=bnow.ans+ynow.ans+up (Op[i]);//The current plus push box Plus is now pushed next to each other; theMark[beed.x][beed.y][i] =true; the Bb.push (beed); -                } the            } the        } the      }94    return false; the } the intMain () the {98 intt,k=1; About  while(SCANF ("%d%d", &m,&n) && m+N) - {101memset (Mark,false,sizeof(Mark));102      for(intI=1; i<=m;i++)103          for(intj=1; j<=n;j++)104     { thescanf"%c",&map[i][j]);106         if(Map[i][j] = ='S')107         {108Sx=i;sy =J;109         } the         if(Map[i][j] = ='T')111         { thetx = I;ty =J;113         } the         if(Map[i][j] = ='B') the         { theBX = I;by =J;117         }118     }119printf"Maze #%d\n", k++); -         if(BFS1 ()) printf ("%s\n\n", Bnow.ans.c_str ());//One less line, WA.121            Elseprintf"impossible.\n\n");122 }123   return 0;124}

POJ 1475 pushing Boxes push box (double 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.