(BFS) Hdoj1242-rescue

Source: Internet
Author: User

Title Address

Beginner BFS, the first time to use BFS to do the problem. The problem is a basic BFS model, which requires a little attention to +1 of the guard time, and the shortest time rather than the number of steps.

1#include <cstdio>2#include <cstring>3#include <queue>4 #defineS 2055 #defineINF 1000006 using namespacestd;7 structLo//build a structure, store point coordinates, and some way to get here. Number of steps and time8 {9     intX,y,step,time;Ten }; OneQueue<lo> Q;//set up a queue and store nodes. A intmintime[s][s],dir[4][2]={{0,1},{0,-1},{-1,0},{1,0}},m,n,si,sj,ei,ej;//Si, SJ storage starting point, mintime storage to a certain point in the shortest time, initialized to INF - CharTem[s][s];//Store Map - intBFS (lo x) the { -     inti; - Q.push (x); - Lo hd; +      while(!q.empty ())//To find the optimal solution, you need to wait until the queue is empty to stop -     { +Hd=q.front ();//take out the queue header and proceed from the node of the queue header.  A Q.pop (); at          for(i=0;i<4; i++) -         { -             intx=hd.x+dir[i][0],y=hd.y+dir[i][1]; -             if(x>=0&&x<m&&y>=0&&y<n&&tem[x][y]!='#')//If one step in one direction is feasible, then it is possible to determine whether this is the shortest. If yes, then press in -             { - Lo t; int.x=x;t.y=y;t.time=hd.time+1; t.step=hd.step+1; -                 if(tem[x][y]=='x') t.time++;//if it's a guard, it's +1 hours. to                 if(T.time<mintime[x][y])//there may be a shorter way to get here, but since each is put in a queue, it will continue from here . +{mintime[x][y]=T.time; - Q.push (t); the                 } *             } $         }Panax Notoginseng     } -     returnMintime[ei][ej];//Back to answers the } + intMain () A { the      while(~SCANF ("%d%d",&m,&N)) +     { -memset (TEM,0,sizeof(TEM)); $         intI,j,an; $          for(i=0; i<m;i++) -         { -scanf"%s", Tem[i]); the         } -          for(i=0; i<m;i++)Wuyi         { the              for(j=0; j<n;j++) -             { WuMintime[i][j]=inf;//initialization time is INF -                 if(tem[i][j]=='a') About                 { $ei=i;ej=J; -                 } -                 Else if(tem[i][j]=='R') -                 { Asi=i;sj=J; +                 } the             } -         } $ Lo t; thet.x=si;t.y=sj;t.step=0; t.time=0; mintime[si][sj]=0; thean=BFS (t); the         if(an<INF) theprintf"%d\n", an); -         Else inprintf"Poor ANGEL have to stay in the prison all his life.\n"); the     } the     return 0; About}

(BFS) Hdoj1242-rescue

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.