(BFS) Poj2935-basic Wall Maze

Source: Internet
Author: User

Title Address

The difference between the topic and the most basic BFS maze is that there are some obstacles that can be created by creating a three-dimensional array that marks a place where obstacles cannot go. The other point is the output path, which establishes a pre variable that points to the previous subscript when constructing the struct. Such backtracking (the method is very classical) can be smooth output.

This problem is really very small, but I spent nearly two hours before the smooth AC, is now too low level, to study hard really is a lot of AH. Whatever you do, try your best.

1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <queue>5 using namespacestd;6 structGrid7 {8     intx, y;//coordinates9     intPre//to retrace the previous subscriptTen     intdir; One}point[5025]; A intsi,sj,ei,ej,a[4],dir[Ten][Ten][4],direction[4][2]={{-1,0},{0,1},{1,0},{0,-1}},vi[8][8]; - intBFS () - { theMemset (VI,0,sizeof(vi)); -     intFront=0, tail=1; -point[front].x=si; -point[front].y=SJ; +point[front].pre=-1; - grid temp,temp2; +vi[si][sj]=1; A      while(front<tail) at     { -temp=Point[front]; -          for(intI=0;i<4; i++) -         { -             if(dir[temp.x][temp.y][i]==1){Continue;} -             Else in             { -temp2.x=temp.x+direction[i][0]; totemp2.y=temp.y+direction[i][1]; +Temp2.dir=i; -                 if(Vi[temp2.x][temp2.y]) the                     Continue; *                 Else $                 {Panax NotoginsengTemp2.pre=Front; -vi[temp2.x][temp2.y]=1; thepoint[tail++]=Temp2; +                     if(temp2.x==ei&&temp2.y==EJ) A                         return(tail-1); the                 } +             } -         } $front++; $     } - } - voidPrintintX//the classic output mode the { -     if(x>0)Wuyi         { the print (point[x].pre); -         if(point[x].dir==0) Wu         { -printf"N"); About         } $         Else if(point[x].dir==1) -         { -printf"E"); -         } A         Else if(point[x].dir==2) +         { theprintf"S"); -         } $         Elseprintf"W"); the         } the  the } the intMain () - { in      while(SCANF ("%d%d",&sj,&si)) the     { the         intI,j,k,an; About         if(si==0&&sj==0) the              Break; thescanf"%d%d",&ej,&ei); thememset (dir,0,sizeof(dir)); +          for(i=1; i<=6; i++) -         { thedir[1][i][0]=1;Bayidir[6][i][2]=1; thedir[i][1][3]=1; thedir[i][6][1]=1; -         } -          for(j=0;j<3; j + +){ thescanf"%d %d%d%d", &a[1],&a[0],&a[3],&a[2]);//Note that the data is different from the usual the  the         if(a[0]==a[2]) the         { -              for(K=min (a[1],a[3])+1; K<max (a[1],a[3])+1; k++) the             { thedir[a[0]+1][k][0]=1; thedir[a[0]][k][2]=1;94             } the         } the         Else the         {98              for(K=min (a[0],a[2])+1; K<max (a[0],a[2])+1; k++) About             { -dir[k][a[1]+1][3]=1;101dir[k][a[1]][1]=1;102             }103         }104     } thean=BFS ();106 print (an);107Puts"");108     }109     return 0; the}

(BFS) Poj2935-basic Wall Maze

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.