"Bfs+ priority queue" Poj2312-battle City

Source: Internet
Author: User

Ideas

The topic "can be launched in a straight line to break the brick wall" may confuse many people, in fact, can be interpreted as equivalent to "through the brick wall of time for 2 units", so the problem is solved. The first encounter can not be very good grasp, the second basic can be used as a water problem.

"Error Point"

1. Do not use the bare BFS. The practical idea of wide search is to put the shortest arrival time in the first team, so that the first time to reach the end is the minimum value. The time through the brick wall is two units, which may not be the time minimum after passing through the brick wall. Use a priority queue to solve this problem.

2.c++ Memset is initialized to memset (vis,0,sizeof (VIS)), and many people may write memset (vis,sizeof (Vis), 0).

1#include <iostream>2#include <cstdio>3#include <iostream>4#include <queue>5 using namespacestd;6 Const intmaxn= -+5;7 structRec8 {9     intX,y,cost;Ten     BOOL operator< (ConstRec &x)Const One     { A         return(Cost >x.cost); -     } - }; the CharMAP[MAXN][MAXN]; - intM,n,yx,yy; -  - voidInit () + { - GetChar (); +      for(intI=0; i<m;i++) A     { at          for(intj=0; j<n;j++) -         { -scanf"%c",&map[i][j]); -             if(map[i][j]=='Y') -             { -yx=i;yy=J; in             } -         } to GetChar (); +     } - } the  * intBFS () $ {Panax Notoginseng     intVIS[MAXN][MAXN]; -     intdx[4]={0,0,1,-1}; the     intdy[4]={1,-1,0,0}; +Priority_queue<rec>que; Amemset (Vis,0,sizeof(Vis)); thevis[yx][yy]=1; + Rec now; -now.x=yx;now.y=yy;now.cost=0; $ Que.push (now); $      while(!que.empty ()) -     { -Rec head=que.top (); the         if(map[head.x][head.y]=='T')return(head.cost); - Que.pop ();Wuyi          for(intI=0;i<4; i++) the         { -             inttempx=head.x+dx[i],tempy=head.y+Dy[i]; Wu             if(tempx<0|| tempx>=m| | tempy<0|| tempy>=n| | map[tempx][tempy]=='R'|| map[tempx][tempy]=='S'|| Vis[tempx][tempy])Continue; -vis[tempx][tempy]=1; Aboutnow.x=tempx;now.y=Tempy; $now.cost=head.cost+1; -             if(map[tempx][tempy]=='B') now.cost++; - Que.push (now); -         } A     } +     return(-1); the } -  $ intMain () the { the      while(SCANF ("%d%d", &m,&n)! =EOF) the     { the         if(M==n && n==0) Break; - init (); inCOUT&LT;&LT;BFS () <<Endl; the     } the     return 0; About}

"Bfs+ priority queue" Poj2312-battle City

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.