POJ 2312 (priority queue +bfs)

Source: Internet
Author: User

        Battle City             Descriptionmany of us had played the game "Battle City" in our childhood, and some people (like me) even often play it on Computer now.

What's discussing is a simple edition of this game. Given A map that consists of empty spaces, rivers, steel walls and brick walls only. Your task is to get a bonus as soon as possible suppose that no enemies would disturb you (see the following picture).

Your tank can ' t move through rivers or walls, but it can destroy brick walls by shooting. A brick wall'll be turned to empty spaces when you hit it, however, if your shot hit a steel wall, there'll be no Da Mage to the wall. In all of your turns, you can choose-to-move to a-neighboring (4 directions, not 8) empty space, or shoot in one of the F Our directions without a move. The shot would go ahead in that direction, until it go out of the the map or hit a wall. If the shot hits a brick wall, the wall would disappear (i.e., in this turn). Well, given the description of a maps, the positions of your tank and the target, how many turns would you take at least to Arrive there?

Input

The input consists of several test cases. The first line of all test case contains, integers m and n (2 <= m, n <= 300). Each of the following M lines contains N uppercase letters, each of the which are one of ' Y ' (You), ' T ' (target), ' S ' (Steel WA ll), ' B ' (brick wall), ' R ' (river) and ' E ' (empty space). Both ' Y ' and ' T ' appear only once. A test Case of M = N = 0 Indicates the end of input, and should not being processed.

Output

For each test case, please output the turns to least in a separate line. If you can ' t arrive at the target, output "-1" instead.

Sample Input

3 4ybebeeresste0 0

Sample Output

8
1 /*as the number of steps encountered brick walls will increase by 2, so the priority queue, priority selection of the minimum number of steps to expand*/2#include <cstdio>3#include <queue>4#include <cstring>5 using namespacestd;6 7 Charmap[305][305];8 BOOLvisit[305][305];9 intSx,sy,ex,ey,m,n;Ten intd[4][2]={1,0,0,1,-1,0,0,-1}; One  A struct Point - { -     intx; the     inty; -     intStep; -     BOOL operator< (ConstPoint &temp)Const -     { +         returnStep>Temp.step; -     } + }; A  at intBFS () - { - Point init; -init.x=SX; -init.y=Sy; -init.step=0; inPriority_queue<point>Q; - Q.push (init); tovisit[init.x][init.y]=true; + Point Node,newnode; -      while(!q.empty ()) the     { *Node=q.top (); $ Q.pop ();Panax Notoginseng         if(node.x==ex&&node.y==ey) -         returnNode.step; the          for(intI=0;i<4; i++) +         { Anewnode.x=node.x+d[i][0]; thenewnode.y=node.y+d[i][1]; +             if(newnode.x>=0&&newnode.x<m&&newnode.y>=0&&newnode.y<n&&!Visit[newnode.x][newnode.y]) -             if(map[newnode.x][newnode.y]!='S'&&map[newnode.x][newnode.y]!='R') $             { $                 if(map[newnode.x][newnode.y]=='B') -newnode.step=node.step+2; -                 Else thenewnode.step=node.step+1; - Q.push (newnode);Wuyivisit[newnode.x][newnode.y]=true;//This is the beginning of the writing outside has been tle the             } -         } Wu     } -     return-1; About } $  - intMain () - { -     //freopen ("In.txt", "R", stdin); A     inti,j; +      while(SCANF ("%d%d", &m,&n), m| |N) the     { -memset (Visit,false,sizeof(visit)); $ GetChar (); the          for(i=0; i<m;i++) the         { the              for(j=0; j<n;j++) the             { -scanf"%c",&map[i][j]); in                 if(map[i][j]=='Y') thesx=i,sy=J; the                 Else if(map[i][j]=='T') Aboutex=i,ey=J; the                 Else; the             } the GetChar (); +         } -         intans=BFS (); theprintf"%d\n", ans);Bayi     } the     return 0; the}

POJ 2312 (priority queue +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.