This problem, simple BFS can be done. The approximate meaning of the topic is the shortest time from the r of the map to reach a.
In the beginning, with the normal queue to do, the result of memory is super, the reason is N and M maximum 200; a normal queue wastes a lot of memory, so you should use a priority queue instead.
The following is the code for AC:
#include <iostream> #include <queue> #include <cstdio>using namespace Std;class data{public:int x, Y, cost;friend bool Operator < (data A, data b)//heap {return b.cost < a.cost;} by minimum heap; int N, M;char map[205][205];int SX, Sy, EX, ey;int xy[4][2] = {1,0,-1,0,0,1,0,-1};int BFS () BFS Search map, traversed by the mark #{priority_queue<data>que;data temp;temp.x = SX; temp.y = sy; temp.cost = 0;que.push (temp); map[s X][sy] = ' # ', while (!que.empty ()) {temp = Que.top (); Que.pop (); if (temp.x = = ex && temp.y = ey) return temp.cost;int F X, fy;for (int i = 0; i < 4; i++)//Four directions Search {FX = temp.x + xy[i][0];fy = temp.y + xy[i][1];if (FX >= 0 &am p;& FX < N && fy >= 0 && fy < M && map[fx][fy]! = ' # ')//determine if symbol condition {if (map[fx][fy] = = '.' || Map[fx][fy] = = ' a ')//judge whether it is a guard, {data te;te.x = FX; te.y = fy; te.cost = temp.cost + 1;que.push (TE); map[fx][fy ] = '#';} Else Is the guard, need two unit time {data te;te.x = FX; te.y = fy; te.cost = temp.cost + 2;que.push (TE); map[fx][fy] = ' # ';}}} return-1;} int main () {freopen ("Data.txt", "R", stdin), while (scanf ("%d%d", &n, &m)! = EOF) {GetChar (); for (int i = 0; i < N; i++) {for (int j = 0; J < M; J + +) {scanf ("%c", &map[i][j]), if (map[i][j] = = ' R ') {SX = I;sy = j;} if (map[i][j] = = ' a ') {ex = I;ey = j;}} GetChar ();} int ans = BFS (); if (ans = =-1) printf ("Poor ANGEL have to stay in the prison all his life.\n"); elseprintf ("%d\n", ans);} return 0;}
Hangzhou Electric acm1242--rescue~~bfs+ Priority queue