"STL" stack + queue + Priority Queue (detailed) + Rescue action

Source: Internet
Author: User
Tags stack pop

First, stack

Stack, aka stacks, is a linear table in which operations are limited. The limitation is that only one end of the table is allowed to insert and delete operations. This end is called the top of the stack, and the opposite end is called the bottom of the stack. Inserting a new element into a stack, also known as a stack, a stack, or a stack, is to put a new element on top of the stack, making it a new stack top element, deleting an element from a stack, or making a stack or fallback, by removing the top element of the stack and making its adjacent elements a new stack top element.

Say the popular point, is a mouth without anus of data structure cough cough ... is a data structure that satisfies a "last in, first out" rule. There are two types of push and pop operations. Push: Pushes elements onto the top of the stack pop: eject elements from the top of the stack

Stack definition in header file <stack>, using "stack<int> S" to declare a stack

↑_↑ and above are very basic

Second, the queue

A queue is a special linear table, except that it allows for deletion only at the front end of the table (front), but in the back-end (rear) of the table, and, like the stack, the queue is a linear table with limited operations.      The end of the insert operation is called the tail of the queue, and the end of the delete operation is called the team header. To speak a little more popular is awith a mouth and an anus. Cough ... is a fair queue that complies with the FIFO principle. Queue definition in header file <queue>, with "queue<int> Q" declaration of a queue, with push (), pop () to team out, front () take the first element of the team, but do not delete √↑_↑ above or is very basic   three, The priority queue is in the priority queue, and the element is given a priority. When an element is accessed, the element with the highest priority is deleted first.  The priority queue has the behavior characteristics of the highest first in, largest out. Priority queue definition in header file <queue>, use "Priority_queue<int> Q" to declare a queue with push (), pop () to team up, top () take the first element of the team, but do not delete √empty () Whether the queue is empty with a priority queue is the time when we must first define a priority for each element, which requires overloaded operators. For example, we want to define a priority queue with a "single-digit large priority" that can be written ↓_↓
 /*   defines a struct CMP, overloaded "()" Operator with "priority_queue <int, Vector<int>, cmp> q "define  */ struct   cmp{ bool   Operator  () (const  int  A, const  int  b) // Span style= "COLOR: #008000" >a the precedence value is less than B returns true  const   { return  a% 10  > B% 10     ; }};

If you want to define a priority queue with a "smaller integer precedence value", you can use this directly: Priority_queue<int, Vector<int>, grater<int> >q

Then there's a problem ↓_↓

openjudge-4980: Description of the 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
Sample input
8#@#####@#@a#@@[email protected]#@@#[email protected]@@@@#@@#@##@@@##@@@#@@@@@@@@@@@@@@ 13  protected]@##[email protected]#[email protected] #xxxx ##@#[email protected]@@ zzfcthotfixz #@ #x #@@[ email protected]# @xxx ###[email protected]#@@##[email protected]@@#@[email protected]@#[ email protected]@@#[email protected]#[email protected]@[email protected]#@x#@x#x#@@##@@x#@ Xx#[email protected]@x##@@@#@[email protected]@[email protected]@##[email protected]@@x#xx #@@ Zzfcthotfixz #@@[email protected]@#@[email protected]@@[email protected]#@#[email protected]#@# xxxxx##@@x##[email protected]@@#[email protected]####@@@x#x##@#@ #xxx #@ #x ##[email protected]@#[ Email protected]@@[email protected]#@#[email protected]######[email protected]#@[email  protected]@@@##@x#xx#[email protected] #xx #@#### #x # @xxx ##@#@x# #x # #x #@x#@a#[email protected]##@# @##[email proTected]#@@[email protected]x#x#@[email protected] #x #@##@[email protected]#[email protected ]# #x # #xx #@#[email protected]@#[email protected]@#@## #x ##[email protected]#@@#@@[email  Protected]@@[email protected]@@@##@@[email protected] @xx #[email protected]### @xxx #@ #x #@@###@#@# #@x#@[email protected]#@@#@@#@#[email protected] #x #x###@[email protected] @xxx ####[email  protected]##@x### #xx #@x#x#@x#x######@@#[email protected] #xxxx #[email protected]@@ zzfcthotfixz #x#####@
Sample output
137

After getting this problem can find is a very simple wide search, with time as priority, code as follows ↓_↓
#include <stdio.h>#include<iostream>#include<queue>using namespacestd;intN, M;intdx[4] = {1,-1,0,0}, dy[4] = {0,0,1,-1};Charmap[201][201];BOOLFlag;structNode//overloaded operator, time-priority{    intx, y, step; FriendBOOL operator<(Node A, Node B) {returnA.step >B.step; }}www, Mmm;priority_queue<node>Q;intMain () {intI, J, S; intSX, SY; scanf ("%d", &s);  while(s--)    {         while(!q.empty ()) Q.pop ();//Attention!! Be sure to empty the queue, here wrote the wrong tune an afternoon I ... The machine room appeared wild crash cake ...scanf"%d%d", &n, &m);  for(i =1; I <= N; i++)        {             for(j =1; J <= M; J + +) {cin>>map[i][j];//Don't ask me why I use CIN →_→, I'm just lazy                if(Map[i][j] = ='R') {sx = i; sy =J;} }} www.x= SX; Www.y = sy; Www.step =0; Map[sx][sy] ='#';        Q.push (WWW); Flag=0;  while(!q.empty ())//the queue is not empty{www= Q.top ();//take the first element of the teamQ.pop ();//Popup Team first element             for(i =0; I <=3; i++) {mmm.x= www.x+Dx[i]; Mmm.y= www.y+Dy[i]; Mmm.step= www.step+1; if(mmm.x<1|| mmm.y<1|| mmm.x>n| | MMM.Y&GT;M)Continue; if(MAP[MMM.X][MMM.Y] = ='#')Continue; if(MAP[MMM.X][MMM.Y] = ='@') {Q.push (MMM);}//elements in the queue                if(MAP[MMM.X][MMM.Y] = ='x') {mmm.step++; Q.push (MMM);} if(MAP[MMM.X][MMM.Y] = ='a') {printf ("%d\n", Mmm.step); Flag =1; Break;} MAP[MMM.X][MMM.Y]='#'; }            if(flag) Break; }        if(!flag) printf ("impossible\n"); }    return 0;}

Basic usage of priority queue end ~\ (≧▽≦)/~

"STL" stack + queue + Priority Queue (detailed) + Rescue action

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.