Nyoj 284 Tank War "BFS"

Source: Internet
Author: User

Tank WarsTime limit:MS | Memory limit:65535 KB Difficulty:3
Describe
Many of us had played the game "Battle City" in our childhood, and some people (like me) even often play it on computer no W.
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



The problem is based on the maze of some changes, that the map can walk on the point may be time-consuming 1, it may take time 2. Then, elements in the out of the queue, not simply in accordance with the previous order out of the team, and should let the shortest time first out of the team, so that you can ensure that the first point must be the shortest time, then the end of the search time also must be minimal. Now looking back, the maze problem is not considered this problem, because the first team to the point of time must not be greater than the team after the queue.
So, how to make the shortest time first out of the team? ----------STL has helped us out,
Priority_queue;
Priority Queue +bfs
Overloaded method One:
1 structnode2 {3     intx, y;4     intStep;5 };6priority_queue<node>q;//The comparison rules of the elements in the priority queue are sorted by the value of the elements from large to small by default;7 8 BOOL operator< (ConstNode &a,ConstNode &b)//the parentheses are const and must also be references9 {Ten     returnA.step > B.step;//sort from small to large. Overload less than sign. Because the default is from big to small One}

Overloaded Method Two:

1 structnode2 {3     intx, y;4     intStep//define a priority queue5FriendBOOL operator<(Node A, Node B)6{//The " >" is used from small to large, and if you want to sort from large to small, the "<" number7         returnA.step > B.step;//sort from small to large8     }9 }; Tenpriority_queue<node>q;//The comparison rules of the elements in the priority queue are sorted by the value of the elements from large to small by default;


Remember: from small to large sorted by the ">", if you want to sort from large to small, the "<" number;

AC Code:
1#include <cstdio>2#include <queue>3#include <string.h>4 using namespacestd;5 #defineMax 3106 intN, M, SX, SY, ex, EY;7 Charmap[305][305];8 intdx[4]={0,0,1, -1};9 intdy[4]={1, -1,0,0};Ten structnode{ One     intx, y, step; AFriendBOOL operator<(Node A, Node B) -     { -         returnA.step > B.step;//the number of steps is less priority the     } - }a, B; - intJudgeintXinty) - { +     if(X <0|| X >= m | | Y <0|| Y >=N) -         return false; +     if(Map[x][y] = ='R'|| Map[x][y] = ='S') A         return false; at     return true;  - } - BOOLBFS () - { -     intFlag =0; -Priority_queue<node>Q; ina.x = SX; A.Y = sy; A.step =0; - Q.push (a); to      while(!q.empty ()) +     { -b =q.top (); the Q.pop (); *         if(b.x = = Ex && b.y = =ey) ${flag =1; Break; }Panax Notoginseng          for(intK =0; K <4; k++)  -         {         thea.x = b.x +Dx[k]; +A.Y = B.y +Dy[k]; A             if(judge (a.x, A.Y)) the             { +                 if(MAP[A.X][A.Y] = ='B') -A.step = B.step +2; $                 Else $A.step = B.step +1; -MAP[A.X][A.Y] ='S'; - Q.push (a);  the             } -         }        Wuyi     } the     if(flag) printf ("%d\n", b.step); -     Elseprintf"-1\n"); Wu } - intMain () About { $      while(SCANF ("%d%d", &m, &n)! =EOF) -     { -         if(m + N = =0) Break; -          for(inti =0; I < m; i++) A         { +scanf"%s", &map[i]); the              for(intj =0; J < N; J + +) -             { $                 if(Map[i][j] = ='Y') the{sx = i; sy =J; } the                 if(Map[i][j] = ='T') the{ex = i; ey =J; } the             } -         }     in BFS (); the     } the     return 0; About}

Nyoj 284 Tank War "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.