Title Link: http://poj.org/problem?id=2312
Battle City
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 7085 |
|
Accepted: 2390 |
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
Test instructions: How long does it take to find a minimum from Y to t?
The first is to seek the shortest time, with BFS. And because the weights of each point may be different, you need a priority queue ~ ~
PS: First time with STL priority queue, good convenient ...
AC Code:
#include <iostream> #include <cstring> #include <queue> #define N 305 using namespace Std;
int n,m,sx,sy,ex,ey,visit[n][n];
int dir[][2]={{0,1},{0,-1},{1,0},{-1,0}};
Char Chess[n][n];
struct node{int x,y,s;
friend bool operator < (Node A,node b) {return a.s>b.s;
}
}; bool OK (int x,int y) {if (x>=0&&x<m&&y>=0&&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+dir[i][0],dy=f.y+dir[i][1];
if (OK (dx,dy) &&visit[dx][dy]) {visit[dx][dy]=0;
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 () {Cin.sync_with_stdio (false); while (cin>>m>>n&& (m| |
N)) {for (int i=0;i<m;i++) for (int j=0;j<n;j++) {cin>>chess[i][j];
if (chess[i][j]== ' Y ') {sx=i;sy=j;
chess[i][j]= ' S ';
} else if (chess[i][j]== ' T ') {ex=i;ey=j;
}} cout<<bfs () <<endl;
} return 0;
}
"Reprint Please specify the source"
Author: mummyding
Source: http://blog.csdn.net/mummyding/article/details/43676441