Battle City
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 7897 |
|
Accepted: 2640 |
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
Source
POJ Monthly, Lu Xiao
http://blog.csdn.net/furturerock/article/details/5568305 feel BFs summed up very good, here paste a link
Test instructions: Just like the game we played when we were kids, it's important to note that the time it takes to meet B is different from other places and requires a priority queue
#include <stdio.h> #include <string.h> #include <queue> #include <algorithm> using namespace std
;
int sx,sy,ex,ey,n,m,visit[305][305];
int dis[][2]={{0,1},{0,-1},{1,0},{-1,0}};//search 4 directions struct Node {int x,y,s;
friend bool operator< (node A,node b) {return a.s>b.s;
};//because the weights of each point may not be the same, so here can not use the general queue, to use the priority queue char chess[305][305]; bool OK (int x,int y)//Determine if the point can go {if (x>=0&&y>=0&&x<m&&y<n&&chess[x][y]!=
' S ' &&chess[x][y]!= ' R ') return true;
return false;
} int BFs () {priority_queue<node>q;
memset (visit,-1,sizeof (visit));
visit[sx][sy]=0;
Node head={sx,sy,0};
Q.push (head);
while (!q.empty ()) {node f=q.top ();
Q.pop ();
if (F.x==ex&&f.y==ey) return F.S; for (int i=0;i<4;i++) {int dx=f.x+dis[i][0],dy=f.y+dis[i][1];//search in four directions if (OK (dx,dy) &&VISIT[DX]
[DY]) {visit[dx][dy]=0;//marks whether the point goesover int temp=0;
if (chess[dx][dy]== ' B ') temp=2;
else temp=1;
Node Tmp={dx,dy,f.s+temp};
Q.push (TMP);
}}} return-1; } int main () {while (scanf ("%d%d", &m,&n), m+n) {for (int i=0;i<m;i++) {scanf ("%s", chess[
I]);
for (int j=0;j<n;j++) {if (chess[i][j]== ' Y ')//start {sx=i;
Sy=j;
chess[i][j]= ' S ';
} else if (chess[i][j]== ' T ')//endpoint {ex=i;
Ey=j;
}}} printf ("%d\n", BFS ());
} return 0;
}