Tank WarsTime limit: Ms | Memory limit: 65535 KB Difficulty: 3 Description Many of us had played the game "Battle City" in our childhood, and some people (like me) even ofte n 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
Analysis: Very water BFS, one thing to note is that each step takes a different time, so use priority queue to deal with.
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std;
Char map[400][400];
int n, m;
int SX, SY;
int dir[][2] = {0, 1, 0,-1, 1, 0,-1, 0};
int vis[400][400];
struct node {int x, y, t;
friend bool Operator < (Node A, Node B) {return a.t > b.t;
}
}; BOOL Check (int x, int y) {if (x >= 0 && x < n && y >= 0 && y < m && map[x][
Y]! = ' S ' && map[x][y]! = ' R ') return true;
else return false;
} void BFs () {memset (Vis, 0, sizeof (VIS));
priority_queue<node>q;
Node St, Ed;
St.x = SX;
St.y = sy;
st.t = 0;
VIS[ST.X][ST.Y] = 1;
Q.push (ST);
while (!q.empty ()) {st = Q.top ();
Q.pop ();
if (map[st.x][st.y] = = ' T ') {printf ("%d\n", st.t);
Return
} for (int i = 0; i < 4; ++i) {ed = st;
Ed.x + = dir[i][0]; Ed.y + = dir[i][1];
if (!check (ed.x, ed.y)) continue;
if (map[ed.x][ed.y] = = ' B ') ed.t + = 2;
else ed.t + = 1;
if (!vis[ed.x][ed.y]) {VIS[ED.X][ED.Y] = 1;
Q.push (ed);
}}} printf (" -1\n"); } int main () {while (scanf ("%d%d", &n, &m), n | | m) {for (int i = 0; i < n; ++i) {scan
F ("%s", Map[i]);
for (int j = 0; j < m; ++j) if (map[i][j] = = ' Y ') {sx = i;
sy = j;
}} BFS ();
} return 0;
}