Battle City
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 7889 |
|
Accepted: 2635 |
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?
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
Ac-code:
#include <cstdio> #include <queue> #include <cstring> using namespace std;
int dx[]={0,1,-1,0};
int dy[]={1,0,0,-1};
int n,m,vis[305][305];
Char map[305][305];
struct node {int x,y,step;
friend bool Operator < (node A,node b) {return a.step>b.step;
}}p,temp;
priority_queue<node>q;
void BFs (int ex,int ey) {memset (vis,0,sizeof (VIS));
while (!q.empty ()) Q.pop ();
P.x=ex;
P.y=ey;
P.step=0;
Vis[ex][ey]=1;
Q.push (P);
while (!q.empty ()) {p=q.top ();
Q.pop ();
for (int i=0;i<4;i++) {temp.x=p.x+dx[i];
Temp.y=p.y+dy[i];
temp.step=p.step+1; if (temp.x<n&&!vis[temp.x][temp.y]&&temp.x>=0&&temp.y<m&&temp.y>=0 &&map[temp.x][temp.y]!= ' S ' &&map[temp.x][temp.y]!= ' R ') {if (map[temp.x][temp.y]== ' B ') temp.step
++;
if (map[temp.x][temp.y]== ' T ') {printf ("%d\n", temp.step);
Return
} vis[temp.x][temp.y]=1;
Q.push (temp); }}} printf (" -1\n");
} int main () {int i,j,ex,ey; while (scanf ("%d%d", &n,&m), n| |
m) {for (i=0;i<n;i++) {scanf ("%s", Map[i]);
for (j=0;j<m;j++) if (map[i][j]== ' Y ') {ex=i;
Ey=j;
}} BFS (Ex,ey);
} return 0;
}