Battle City
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 6496 |
|
Accepted: 2176 |
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
Originally thought to be a simple BFS but to do this is to use the priority queue for optimization test instructions: The Tank meets the River (R), the wall (S) can not walk, the other may go, but encountered B to consume 2 of the time, Encounter e need to consume 1 of the time to start not much thought, direct BFS has been WA ... When encountering B, it consumes 2 of the time because I have to stay a second in the encounter B, in a second because the BFS is traversed by layers, so although the answer is that the time spent is different, so it should be processed (in fact, the priority is not the same) two ways, One is to use the priority queue method Use the priority queue to get the queue to the point where the starting time value is the smallest. (To think about ...) ) Code:
#include <iostream> #include <string.h> #include <queue> #include <algorithm> #include <
Stdio.h> using namespace std;
Char map[1005][1005];
int Sx,sy,ex,ey;
int vis[1005][1005];
int dir[4][2] = {0, 1, 0,-1, 1, 0,-1, 0};
int n,m;
struct Node {int x,y,cost;};
BOOL operator < (const node &a, const node &b) {return a.cost > b.cost;}
int BFS (int sx,int sy) {Node node,now,next;
cout<<n<<m<<endl;
int x, y;
Priority_queue<node> que;
while (!que.empty ()) {Que.pop ();
} node.x=sx;
Node.y=sy;
node.cost=0;
Vis[node.x][node.y]=1;
Que.push (node);
while (!que.empty ()) {now=que.top ();
cout<<map[now.x][now.y]<<endl;
Que.pop ();
cout<<now.x<< "" <<ex<<endl;
cout<<now.y<< "" <<ey<<endl;
if (Now.x==ex&&now.y==ey) { cout<<now.cost<<endl;
return 1;
} for (int i=0;i<4;i++) {//cout<<endl<<endl;
next.x=dir[i][0]+now.x;
NEXT.Y=DIR[I][1]+NOW.Y;
cout<<next.x<< "Next.x" <<endl;
cout<<next.y<< "Next.y" <<endl; if (next.x<n&&next.y<m&&next.x>=0&&next.y>=0&& (map[next.x][next.y]= = ' E ' | | map[next.x][next.y]== ' B ' | | map[next.x][next.y]== ' Y ' | | map[next.x][next.y]== ' T ') &&!vis[next.x][next.y]) {//Cout<<1<<e
Ndl
Vis[next.x][next.y]=1;
if (map[next.x][next.y]== ' B ') next.cost=now.cost+2;
else next.cost=now.cost+1;
Que.push (next);
}}} return 0; } int Main ({while (cin>>n>>m) {if (n==0&&m==0) is break;
memset (vis,0,sizeof (VIS));
memset (map,0,sizeof (map));
ex=ey=sx=sy=-100; for (int i=0;i<n;i++) {for (int j=0;j<m;j++) {cin>>map[i][
J];
if (map[i][j]== ' Y ') {sx=i;
Sy=j;
} if (map[i][j]== ' T ') {ex=i;
Ey=j; }}} if (!bfs (sx,sy)) cout<<-1<&
Lt;endl;
} return 0; }
Kidney pain, kidney pain ..... Also attached is the first method of declaring the priority queue:
struct node
{
int x, y;
int step;
};
priority_queue<node>q; The comparison rules for the elements in the priority queue are sorted by the value of the element from large to small by default,
and bool operator< (const node &A,CONST node &b)//in parentheses is const and must also be a reference
{
return a.step>b.step;//from small to large sort. Overload less than sign. Because the default is from big to small
}
The second type:
struct node
{
int x, y;
int time; Define a priority queue
friend bool operator< (Node A, Node B)
{///from small to large sort with ">", if you want to sort from large to small, use "<"
return A.time> B.time; From small to large sort
}
};
priority_queue<node>q; The comparison rules of the elements in the priority queue are sorted by the value of the elements from large to small by default;
Remember: from small to large sorted by the ">", if you want to sort from large to small, the "<" number;
Priority queues are not just for efficiency drops ~