nyoj-284-Tank War (BFS)

Source: Internet
Author: User
Tags time limit

Tank Wars
Time 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 4
Ybeb
EERE
Sste
0 0
Sample output
8

Test instructions: The figure of Y stands for the beginning, T for the end, s for the iron wall, not destroyed can not be passed, b stands for wood wall, could be destroyed, after the wreck can be passed, R for the river, not wreck also can not be passed, E represents.
Destroying a wooden wall consumes one step, asking at least how many steps can be reached from the beginning to the end.

Code

#include <stdio.h> #include <string.h> #include <queue> #include <iostream> #include <
Algorithm> using namespace std;
Priority queue wide Search const int maxn=305;
const int inf=0x3f3f3f3f;
Char MAP[MAXN][MAXN];
int VIS[MAXN][MAXN];
int dis[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};
int n,m;
int star_x,star_y;
int end_x,end_y;
    struct node {int x;
    int y;
    int time;
    friend bool Operator < (node N1,node n2) {return n1.time>n2.time;

}
};
    int BFS (int star_x,int star_y) {priority_queue<node>q;
    Node Star;
    star.time=0;
    star.x=star_x;
    star.y=star_y;
    memset (vis,0,sizeof (VIS));
    Vis[star_x][star_y]=1;
    Q.push (Star);
        while (!q.empty ()) {star=q.top ();
        Q.pop ();
            for (int i=0; i<4; i++) {node end;
            End=star;
            END.X+=DIS[I][0];

            END.Y+=DIS[I][1]; if (end.x>=1&&end.x<=n&&end.y>=1&&end.y<=m&&vis[end.x][end.y]==0&&map[end.x][end.y]!= ' S ' &&map[end.x][end.y]!= ' R ') {//
                printf ("%d%d\n", END.X,END.Y);
                    if (map[end.x][end.y]== ' E ') {end.time++;
                    Vis[end.x][end.y]=1;
                Q.push (end);
                    } else if (end.x==end_x&&end.y==end_y) {end.time++;
                return end.time;
                    } else if (map[end.x][end.y]== ' B ') {end.time++;
                    end.time++;//here made a change vis[end.x][end.y]=1;
                Q.push (end);            }}}}//for (int i=1, i<=n; i++)//{//for (int j=1; j<=m; j + +)//
printf ("%c", Map[i][j]);
printf ("\ n");
} return-1; } int main () {while (~scanf ("%d%d", &n,&m) &&n&&amP
            M) {for (int i=1; i<=n; i++) {GetChar ();
                for (int j=1; j<=m; J + +) {scanf ("%c", &map[i][j]);
                    if (map[i][j]== ' Y ') {star_x=i; star_y=j;//There may be a problem} else if (map[i][j]== ' T ') {end_x=i
                    ;
                End_y=j;
        }}//GetChar ();
    } printf ("%d\n", BFS (star_x,star_y));
} 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.