Nyoj 284 Tank Wars && POJ 2312 Battle City (wide search + priority queue)

Source: Internet
Author: User
Tags min

Link: click here~~

Test instructions

Description Many 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? Enter 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 4
ybeb
EERE
sste
0 0
Sample output
8

Wide Search (BFS) + priority queue, from 4:00 to write to now, with three methods, gradually become familiar with the writing of this kind of problem, a little feeling of a wide search and write, continue to maintain.

"Problem-solving ideas" wide search + priority queue, enumerate all directions, note that there is a time-consuming difference, so it is easy to think of priority queue (Priority_queue), can also use deep search to try

Code:

#include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> #include <
Queue> using namespace std;
const int MAX = 305;
Char Map[max][max];
int n,m,u,v,s,e;
    struct Node {int x,y,step;
    friend BOOL operator < (const node A,const node B) {return a.step > b.step;
}
};
int dir[4][3]= {{1,0},{0,1},{-1,0},{0,-1}};
    BOOL Judg (Node temp) {int x = temp.x;
    int y = temp.y; if (x<0| | x>=n| | y<0| | y>=m| | map[x][y]== ' S ' | |
    map[x][y]== ' R ') return false;
return true;
} Node Pre,last;
    int BFS () {priority_queue <Node>Q;
    Node temp;
    Q.push (pre); while (!
        Q.empty ()) {pre=q.top ();
        Q.pop ();
        if ((pre.x==last.x) && (PRE.Y==LAST.Y)) return pre.step;
            for (int i = 0; i < 4; i++) {temp.x = pre.x + dir[i][0];
            TEMP.Y = Pre.y + dir[i][1]; Temp.
            Step = pre.step + 1;
         if (JUDG (temp))   {if (map[temp.x][temp.y]== ' B ') temp.step + = 1;
                MAP[TEMP.X][TEMP.Y] = ' S ';
            Q.push (temp);
}}} return-1;
    } void Init () {for (int i = 0; i < n; i++) scanf ("%s", Map[i]);
            for (int i = 0; i< N, i++) for (int j = 0; J < m; J + +) {if (map[i][j] = = ' Y ')
                {pre.x = i;
                Pre.y = j;      
            Pre.step=0;
                } if (map[i][j] = = ' T ') {last.x = i; 
                Last.y = j;
            Last.step=0; }}} int main () {while (scanf ("%d%d", &n,&m), (n| |
        m)) {init ();
        int dist = BFS ();
    printf ("%d\n", Dist);
} return 0;


 }

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.