Hdu 5040 priority queue BFS + pruning, hdubfs

Source: Internet
Author: User

Hdu 5040 priority queue BFS + pruning, hdubfs

Question 09 of Beijing Network Competition: Give a matrix (figure) with a starting point, an ending point, and a searchlight (each with an initial orientation, turning clockwise to 90 degrees per second ), if there is a lamp in front or you are illuminated by the lamp, it takes 3 seconds to move the light.

Use an array of 3D arrays to record whether there is any light at the current time % 4 at the current position, and then prioritize the queue (before the short time) and search for it. Considering that you can go back and forth or stay in the same place, you cannot simply judge and re-pruning: each place can be in up to four statuses! That is, after 4 seconds, the full graph status returns to the same! So if the current status (time % 4) goes down, it will not be used.

#include<iostream>#include<vector>#include<cstdio>#include<algorithm>#include<cstring>#include<string>#include<queue>#include<cmath>using namespace std;struct xy{    int x,y;    int times;    bool operator <(const xy &ttt)const    {        return ttt.times<times;    }};int n;int a[505][505];int b[505][505][4];int vis[505][505][4];int f[4][2]={-1,0,0,1,1,0,0,-1};xy ss; int mints;void bfs(){      priority_queue<xy>q;      q.push(ss);      while(!q.empty())      {          xy cur=q.top();          q.pop();          if(a[cur.x][cur.y]==4&&cur.times<mints)          {             mints=cur.times;break;          }          xy next1=cur;          next1.times=cur.times+1;          if(vis[next1.x][next1.y][next1.times%4]==0)          {              vis[next1.x][next1.y][next1.times%4]=1;              q.push(next1);          }          for(int i=0;i<4;i++)          {              xy next;               next.x=cur.x+f[i][0];               next.y=cur.y+f[i][1];              if(next.x>=0&&next.y>=0&&next.x<n&&next.y<n&&a[next.x][next.y]!=0)                {                     if(b[next.x][next.y][cur.times%4]==1||b[cur.x][cur.y][cur.times%4]==1)                       {                                next.times=cur.times+3;                             if(vis[next.x][next.y][next.times%4]==0)                            {                                      vis[next.x][next.y][next.times%4]=1;                                      q.push(next);                            }                       }                       else                       {                            next.times=cur.times+1;                             if(vis[next.x][next.y][next.times%4]==0)                            {                                      vis[next.x][next.y][next.times%4]=1;                                      q.push(next);                            }                       }                }          }      }}int main(){    int T;     scanf("%d",&T);int cnt=1;    while(T--)    {        scanf("%d",&n);        memset(b,-1,sizeof(b));        memset(vis,0,sizeof(vis));        char tx;char sss[505];        for(int i=0;i<n;i++)        {            scanf("%s",sss);              for(int j=0;j<n;j++)             {                 tx=sss[j];                if(tx=='.')                {                    a[i][j]=1;                }                else if(tx=='T')                {                    a[i][j]=4;                }                else if(tx=='M')                {                    a[i][j]=1;                    ss.x=i;ss.y=j;                    ss.times=0;                }                else if(tx=='#')                {                    a[i][j]=0;                }                else if(tx=='N')                {                    a[i][j]=2;                     for(int k=0;k<4;k++)                     {                         int xx=i+f[k][0];                         int yy=j+f[k][1];                         b[i][j][k]=1;                         if(xx>=0&&yy>=0&&xx<n&&yy<n)                         {                             b[xx][yy][k]=1;                         }                     }                }                else if(tx=='E')                {                     a[i][j]=2;                     for(int k=0;k<4;k++)                     {                         int xx=i+f[k][0];                         int yy=j+f[k][1];                          b[i][j][k]=1;                         if(xx>=0&&yy>=0&&xx<n&&yy<n)                         {                             int ii=k;                             if(ii==0)ii=3;                             else ii--;                             b[xx][yy][ii]=1;                         }                     }                }                else if(tx=='S')                {                     a[i][j]=2;                     for(int k=0;k<4;k++)                     {                         int xx=i+f[k][0];                         int yy=j+f[k][1];                          b[i][j][k]=1;                         if(xx>=0&&yy>=0&&xx<n&&yy<n)                         {                             int ii=k;                             if(ii==0||ii==1)ii=ii+2;                             else ii=ii-2;                             b[xx][yy][ii]=1;                         }                     }                }                else if(tx=='W')                {                     a[i][j]=2;                     for(int k=0;k<4;k++)                     {                         int xx=i+f[k][0];                         int yy=j+f[k][1];                          b[i][j][k]=1;                         if(xx>=0&&yy>=0&&xx<n&&yy<n)                         {                             int ii=k;                             if(ii==3)ii=0;                             else ii++;                             b[xx][yy][ii]=1;                         }                     }                }            }        }         mints=0x3f3f3f3f;        vis[ss.x][ss.y][ss.times]=1;        bfs();        printf("Case #%d: ",cnt++);       if(mints==0x3f3f3f3f)       {           printf("-1\n");       }       else       {           printf("%d\n",mints);       }    }            return 0;}



Priority queue

In C ++, the struct and class keywords are basically the same, but the default access restriction is public.
The former operator <is an internal operator overload of the class, and the latter is a global operator overload.
The latter definition is often declared as a friend of the class. Otherwise, the class cannot be private or protect members and methods.

Priority queue Problems

The reason is that when priority_queue is instantiated, a third parameter is a preference predicate. The default value is less (the second parameter is the container, and the default value is vector). That is to say, the content of priority_queue will be sorted in descending order, in this way, the maximum value has the highest priority. If your value is smaller than 0, the priority is reversed.
Change the definition of q to priority_queue <woca, vector <woca>, greater <woca> q;
In addition, a ">" operator function is added to the end to see how the result is:
Bool operator> (woca m, woca n)
{
If (m. x> n. x)
Return 1;
Else
Return 0;
}

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.