HDU 1242 Rescue (priority queue + bfs)

Source: Internet
Author: User

HDU 1242 Rescue (priority queue + bfs)

Address: HDU 1242

This question has a special place compared to the ordinary bfs. It will consume extra time when soldiers pass through it, that is to say, it is not necessarily the shortest time when the first search is made. You need to search all of them. At this time, the benefits of priority queue are apparent. The priority queue can be used to sort the elements in the queue by time, so that the first time is always short. In this way, the first time found must be short, you don't need to search it all. PS: I did this to learn the priority queue .. It is not a priority queue for this question ..

The Code is as follows;

#include 
 
  #include 
  
   #include 
   
    #include 
    
     #include 
     
      #include 
      
       #include 
       
        #include 
        #includeusing namespace std;int vis[300][300], n, m;char mp[300][300];int jx[]={0,0,1,-1};int jy[]={1,-1,0,0};struct node{ int x, y, ans; bool operator<(const node &a) const{ return ans>a.ans; }};void bfs(int x, int y){ memset(vis,0,sizeof(vis)); priority_queue
         
          q; node f1, f2; f1.x=x; f1.y=y; f1.ans=0; q.push(f1); vis[x][y]=1; while(!q.empty()) { f1=q.top(); q.pop(); if(mp[f1.x][f1.y]=='r') { printf("%d\n",f1.ans); return ; } for(int i=0;i<4;i++) { f2.x=f1.x+jx[i]; f2.y=f1.y+jy[i]; if(f2.x>=0&&f2.x
          
           =0&&f2.y
           
            

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.