Hangzhou Electric acm1242--rescue~~bfs+ Priority queue

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.