Battle City
Time limit:1000ms Memory limit:65536k
Total submissions:9324 accepted:3086
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
Source
POJ Monthly, Lu Xiao
Test Instructions:
Give you a matrix of m rows n columns.
Y represents the starting point, and T represents the endpoint. B, E can go, S, R can not go, b time spent for 2,e 1.
The shortest time to ask Y to T.
Analysis:
Directly is the simple BFS can.
(I forgot to add the end situation to the next state .....) Haematemesis.... )
AC code:
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <
algorithm> #include <cmath> #include <queue> using namespace std;
const int MAXN = 310;
int m,n,sx,sy;
Char G[MAXN][MAXN];
BOOL VIS[MAXN][MAXN];
int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
struct Tank {int x,y,cost;
friend BOOL operator < (const tank &A,CONST tank &b) {return a.cost > b.cost;
}
};
BOOL Check (int x,int y) {if (x < 0 | | x >= m | | y < 0 | | y >= N) return 0;
if (g[x][y] = = ' S ' | | g[x][y] = = ' R ' | | vis[x][y]) return 0;
return 1;
} bool BFs () {memset (vis,0,sizeof (VIS));
Priority_queue<tank> Q;
Tank Now,next; now.x = SX; Now.y = sy;
Now.cost = 0;
Q.push (now);
Vis[sx][sy] = 1;
while (!q.empty ()) {now = Q.top (); Q.pop ();
if (g[now.x][now.y] = = ' T ') {printf ("%d\n", now.cost);
return true;
} for (int i=0;i<4;i++) {next.x = now.x + dir[i][0];
Next.y = Now.y + dir[i][1]; if (check (NEXT.X,NEXT.Y)) {if (g[next.x][next.y] = = ' E ' | | g[next.x][next.y] = = ' T ') next.cost = Now.cost + 1;
Don't forget g[next.x][next.y] = = ' T ' else if (g[next.x][next.y] = = ' B ') Next.cost = Now.cost + 2;
Q.push (next);
VIS[NEXT.X][NEXT.Y] = 1;
}}} return false;
} int main () {while (scanf ("%d%d", &m,&n)!=eof && m) {GetChar ();
for (int i=0;i<m;i++) gets (G[i]);
int f = 0;
for (int i=0;i<m;i++) {if (f) break;
for (int j=0;j<n;j++) {if (g[i][j] = = ' Y ') {sx = i;
sy = j;
f = 1;
Break }}} if (!bfs ()) PrinTF (" -1\n");
} return 0;
}