Battle City
Time limit:1000ms Memory limit:65536k
Total submissions:8597 accepted:2882
Description
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
title Link:http://poj.org/problem?id=2312 Test Instructions
Calculate the shortest time from ' Y ' to ' t ', where ' S ' and ' R ' cannot go, ' E ' takes time of 1, ' B ' takes time to 2.
Solving
priority queue BFS, which takes the node with the least current time to expand during traversal.
#include <iostream> #include <string.h> #include <algorithm> #include <stdio.h> #include < math.h> #include <stack> #include <queue> #include <vector> #define INF 0xFFFFFFFF using namespace St
D
const int MAXN = 305;
int ax, BX, Ay, by;
int m, n;
Char PIC[MAXN][MAXN];
int VIS[MAXN][MAXN];
int dr[4] = { -1,0,1,0};
int dc[4] = {0,1,0,-1};
struct node{int R, c, T; Node () {} node (int r, int c, int t): R (R), C (c), T (t) {} BOOL operator < (const Node & RHS) const{//defined less than, passed in
Come from the current smaller than the true return T > rhs.t;
}
}; int go (int r, int c) {if (r<0 | | r>=m | | c<0 | | c>=n | | pic[r][c] = = ' S ' | | pic[r][c] = = ' R ' | | vis[r][c])
return 0;
else if (pic[r][c] = = ' T ' | | pic[r][c] = = ' E ') return 1;
else return 2;
} void Solve () {priority_queue<node> q;
Node St (Ax, ay, 0);
Q.push (ST);
Vis[ax][ay] = 1;
while (!q.empty ()) {Node u = q.top ();
Q.pop (); for (int i=0; i<4;
i++) {int nextr = U.R + dr[i];
int NEXTC = u.c + dc[i];
int t = Go (NEXTR, NEXTC);
if (t) {VIS[NEXTR][NEXTC] = 1;
Node U1 (nextr, NEXTC, u.t+t);
Q.push (U1);
if (nextr = = BX && NEXTC = = by) {printf ("%d\n", u1.t);
Return
}}}} printf (" -1\n");
} int main () {while (scanf ("%d%d", &m, &n) = = 2 && m && N) {for (int i=0; i<m; i++) {
scanf ("%s", Pic[i]);
for (int j=0; j<n; J + +) {if (pic[i][j] = = ' Y ') {ax = i, ay = j;
} if (pic[i][j] = = ' T ') {bx = i, by = J;
}}} memset (Vis, 0, sizeof (VIS));
if (ax = = bx && ay = = by) {printf ("0\n");
} else{Solve ();
}} return 0; }