vijos-p1340 Rescue Ice-cream (wide search + priority queue)

Source: Internet
Author: User

Test instructions: The shortest time from S to M. ("O" Cannot Walk, ' # ' walk a flower Two units of time, '. ') Walk a unit time for a flower)

Idea: Wide search and Priority queue.

1#include <stdio.h>2#include <string.h>3#include <iostream>4#include <queue>5 #defineMAX 306 using namespacestd;7 8 structPOS9 {Ten     intx; One     inty; A     intStep; - }; -  the BOOL operator< (ConstPOS &a,ConstPOS &b) - { -     returnA.step >B.step; - } +  - Pos SP, EP; + CharMap[max][max]; A intdir[4][2] = {{1,0}, {-1,0}, {0,1}, {0, -1}}, M, N, ti; at  - intBFS () - { -Priority_queue<pos>Q; - Pos temp, t; -temp =sp; inTemp.step =0; - Q.push (temp); to      while(!q.empty ()) +     { -temp =q.top (); the Q.pop (); *         if(Temp.step >=ti) $         {Panax Notoginseng             Continue; -         } the         if(temp.x = = ep.x && temp.y = = ep.y && Temp.step <ti) +         { A             returnTemp.step; the         } +          for(inti =0; I <4; i++) -         { $T.x = temp.x + dir[i][0]; $T.y = temp.y + dir[i][1]; -             if(T.x >=0&& t.x < n && t.y >=0&& T.y < M && Map[t.x][t.y]! ='o') -             { the                 if(Map[t.x][t.y] = ='.') -                 {WuyiT.step = Temp.step +1; theMAP[T.X][T.Y] ='o'; - Q.push (t); Wu                 } -                 Else if(Map[t.x][t.y] = ='#') About                 { $T.step = Temp.step +2; -MAP[T.X][T.Y] ='o'; - Q.push (t); -                 } A                 Else if(Map[t.x][t.y] = ='m') +                 { theT.step = Temp.step +1; -MAP[T.X][T.Y] ='o'; $ Q.push (t); the                 } the             } the         } the     } -     return-1; in } the  the intMain () About { thescanf"%d%d%d", &ti, &m, &n); the     intans; the      for(inti =0; I < n; i++) +     { -          for(intj =0; J < M; J + +) the         {BayiCin>>Map[i][j]; the             if(Map[i][j] = ='s') the             { -sp.x =i; -SP.Y =J; the             } the             if(Map[i][j] = ='m') the             { theep.x =i; -EP.Y =J; the             } the         } the     }94Ans =BFS (); the     if(ans = =-1) the     { theprintf"55555\n");98     } About     Else -     {101printf"%d\n", ans);102     }103     return 0;104}
View Code

vijos-p1340 Rescue Ice-cream (wide search + 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.