"STL" Priority queue Priority_queue detailed +openjudge-4980 rescue operation

Source: Internet
Author: User

First, about the priority queue

Queue This kind of thing the vast majority of oier should not be unfamiliar, or, the queue will not you also learn an egg AH (╯‵-′) ╯︵┻━┻ Cough, colloquially, the queue is a data structure that only allows the deletion of elements from the front end (team header) and the insertion of elements from the back end (tail). The priority queue is a queue that assigns elements in each queue to a priority. When you perform a delete operation, the priority queue deletes the element with the highest priority. What's the use of such a wonderful priority queue, for example, given a sequence of N and M, for each set of queries, we want to find out the smallest number in the deleted sequence, and then add a number to the sequence. The simple idea is to sweep through each query, find the minimum, and the time complexity is O (nm). The priority queue can reduce the complexity of time to O (NLGN) levels. And how did it do it? In fact, a priority queue (also known as a heap) is a fully binary tree with a certain characteristic (that is, the priority that we set) between each child node and the parent node. The nature of the binary tree determines the complexity of the priority queue deletion operation O (LGN).

Second, the priority_queue of STL

Priority queue easy to use, however, it is not good to write _ (: З"∠) _ However, the STL (Standard Template Library standards templates) given a priority queue template ~\ (≧▽≦)/~

Priority_queue defined in the header file <queue>, like the normal queue, Priority_queue has the following actions:

1, Priority_queue<type, Container, functional>q: Create a new priority queue Q, whose type is its data type, Container is the container to hold the data, and functional is the way to compare elements ;

Special note: Container must be an array implementation of the container, default to vector;functional default is operator <. So if we put both container and functional in the default, the priority queue is the biggest heap of a team head element.

2, Q.top (): Return the team head element;

3, Q.pop (): Delete the head element of the queue;

4, Q.push (x): add element x at the tail of the team;

5, Q.empty (): Determines whether the queue is empty (returns True if the queue is empty, otherwise, returns false);

6, Q.size (): Returns the number of elements in the queue.

Iii. description of the OpenJudge-4980 rescue operation

The princess was taken prisoner by the wicked and imprisoned somewhere in the cell. The cell is represented by a matrix of n*m (N, M <= 200). Each item in the matrix can represent a road (@), a wall (#), and a guard (x).
The Valiant Knight (R) decided to be alone to save the Princess (a). We assume that the success of the rescue is expressed as "the knight has reached the place where the princess is". As the guard may be encountered on the road leading to the princess's location, once the knight encounters the Guard, the guard must be killed to move on.
It is assumed that the knight can move up, down, left, and right in four directions, requiring 1 units of time per move, and killing a guard takes an additional 1 units of time. Also assume that the knight is strong enough to kill all the guards.

Given the cell matrix, the princess, the Knight and the Guard in the matrix position, please calculate the success of the rescue operation will take the shortest time.

Input

The first behavior is an integer s that represents the number of groups of data entered (multiple sets of inputs)
followed by S-group data, each set of data entered in the following format
1, two integers represent N and M, (N, M <= 200).
2, followed by n lines, each line has m characters. "@" stands for the road, "a" stands for the princess, "R" stands for the knight, "X" stands for the Guard, "#" stands for the wall.

Output

If the rescue operation succeeds, output an integer that represents the shortest time of the action.
Output "Impossible" if not successful

Original title link →_→ OpenJudge-4980 rescue operation

Read the question we found that this is a wide search. As long as we define the priority by time, we can put the status of each point to the priority queue.

Happy stickers on the code:

1#include <cstdio>2#include <queue>3#include <cstring>4 using namespacestd;5 Const intMaxl= About;6 intdir[]={-1,1,0,0},dir_[]={0,0,-1,1};7 ints;8 intn,m;9 intMAP[MAXL][MAXL];Ten BOOLVIS[MAXL][MAXL]; One intDx,dy; A structnode - { -     intX,y,time; theFriendBOOL operator< (node A,node B) {returnA.time>B.time;} -     //Heavy-duty <, defined as priority, time-small elements placed on the team head - }; -Priority_queue <node> Q;//priority_queue <node,vector<node>,operator <> q; + intMain () - { +scanf"%d",&s); A      while(s--) at     { -          while(!q.empty ()) Q.pop ();//empty Queue Q, note: No use of q.clear () -memset (Vis,false,sizeof(Vis)); -memset (Map,0,sizeof(map)); -scanf"%d%d",&n,&m); -          for(intI=1; i<=n;++i) in         { -             CharC[MAXL]; toscanf"%s", c); +              for(intj=0; j<m;++j) -             { the                 if(c[j]=='a') dx=i,dy=j+1; *                 Else if(c[j]=='R') Q.push (node) {i,j+1,0}), vis[i][j+1]=true; $                 Else if(c[j]=='x') map[i][j+1]=1;//1 stands for the guard.Panax Notoginseng                 Else if(c[j]=='#') map[i][j+1]=2;//2 stands for the wall -             } the         } +          while(!q.empty ()) A         { the             intX=q.top (). X,y=q.top (). y,time=q.top (). Time; +             if(X==dx&&y==dy) Break; -              for(intI=0;i<4;++i) $             { $                 if(x+dir[i]<1|| x+dir[i]>n| | y+dir_[i]<1|| Y+DIR_[I]&GT;M)Continue; -                 if(vis[x+dir[i]][y+dir_[i]]| | map[x+dir[i]][y+dir_[i]]==2)Continue; -vis[x+dir[i]][y+dir_[i]]=true; the                 if(map[x+dir[i]][y+dir_[i]]==0) Q.push (node) {x+dir[i],y+dir_[i],time+1}); -                 Else if(map[x+dir[i]][y+dir_[i]]==1) Q.push (node) {x+dir[i],y+dir_[i],time+2});Wuyi             } the Q.pop (); -         } Wu         if(Q.empty ()) printf ("Impossible"); -         Elseprintf"%d", Q.top (). time); Aboutprintf"\ n"); $     } -     return 0; -}
OpenJudge-4980 rescue Operation

Weakly weak to say, the Konjac Konjac code word is not easy, reproduced please indicate the source http://www.cnblogs.com/Maki-Nishikino/p/6056072.html

"STL" Priority queue Priority_queue detailed +openjudge-4980 rescue operation

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.