HDU 1242-rescue (bidirectional BFS) & amp; (BFS + priority queue)

Source: Internet
Author: User

Link: Rescue


The progress has dropped too much. I always complain that the progress is slower than others... Why don't you try to change it ....


I thought it was a water question. I wanted to try it out. Later I found that it was not a simple search question. BFS was definitely doing something wrong... Later, I found that there were pitfalls in the question.

It is the shortest distance from R to A, "." is equivalent to 1, and "X" is equivalent to 2.


HDU search courseware said that this question is similar to hdu1010. At the beginning, I didn't think it was like pruning, so I switched to two-way BFS 0 Ms a Y!

After checking on the Internet, the cool actually used BFS + as the priority queue... Epiphany


So the search tree in this question can be understood as root as a, connecting several branches, and the leaf of the branches is R. The answer is naturally given to the decision tree with the smallest depth for deep pruning; use the priority queue to control and filter large branches in depth for pruning.

After a few clicks, BFs + priority queue is 15 ms a y. I believe that we can optimize the storage of the ANS, and store the smaller ans with priority. 0 ms is very easy.


Send special data:

3 3

..

X #.

. R.

Print 4



The BFS + priority queue code is as follows:

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <queue>const int N = 210;using namespace std;struct node{    int x, y,ans;   friend bool operator <(const node &a,const node &b)    {        return a.ans>b.ans;    }};int n, m;char ma[N][N];bool vis[N][N];int sx, sy;int mv[4][2] = {{-1,0},{0,1},{0,-1},{1,0}};void BFS(int sx,int sy){    priority_queue<node> q;    node f, t;    f.x = sx, f.y = sy, f.ans = 0;    vis[sx][sy] = true;    q.push(f);    while(!q.empty())    {        t = q.top();         if(ma[t.x][t.y]=='r')            {                cout<<t.ans<<endl;                return ;            }        q.pop();        for(int i=0; i<4; i++)        {            f.x = t.x +mv[i][0];            f.y = t.y +mv[i][1];            if(!vis[f.x][f.y]&&0<=f.x&&f.x<n&&0<=f.y&&f.y<m&&ma[f.x][f.y]!='#')            {                if(ma[f.x][f.y]=='x')               {                 f.ans = t.ans+2;                 q.push(f);               }               else              {                f.ans = t.ans+1;                q.push(f);              }              vis[f.x][f.y] = true;            }        }    }     cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;}int main(){    while(~scanf("%d%d",&n,&m))    {        bool flag = 0;        for(int i=0; i<n; i++)        {           scanf("%s",ma[i]);           if(flag)            continue;            for(int j=0; j<m; j++)            {                if(ma[i][j]=='a')                {                     sx=i;sy=j;flag = true;                     break;                }            }        }        memset(vis,0,sizeof(vis));        BFS(sx,sy);    }    return 0;}

Bidirectional BFS

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <queue>const int N = 210;using namespace std;int mapp[N][N];int vis[N][N];struct node{    int x,y;};int n,m;char ma[210][210];int mv[4][2] = {{1,0},{0,1},{0,-1},{-1,0}};int dis[N][N];void BFS(int sx,int sy,int ex,int ey){    queue<node>q;    node t,f;    memset(vis,0,sizeof(vis));    memset(dis,0,sizeof(dis));    f.x = sx; f.y = sy;    t.x = ex; t.y = ey;    vis[sx][sy] = 1;     vis[ex][ey] = 2;    q.push(f);    q.push(t);    while(!q.empty())    {        t = q.front();        q.pop();        for(int i = 0;i<4;i++)        {            f.x = t.x + mv[i][0];            f.y = t.y + mv[i][1];            if(0<=f.x && f.x <n && 0<=f.y && f.y<m)            {                if(ma[f.x][f.y]=='#') continue ;                if(!vis[f.x][f.y]&& ma[f.x][f.y]=='x')                {                dis[f.x][f.y] = dis[t.x][t.y] + 2;                q.push(f);                vis[f.x][f.y] = vis[t.x][t.y];                }                else if(!vis[f.x][f.y]&& ma[f.x][f.y]=='.')                {                dis[f.x][f.y] = dis[t.x][t.y] + 1;                q.push(f);                vis[f.x][f.y] = vis[t.x][t.y];                }                else if(vis[f.x][f.y]!=vis[t.x][t.y])                {                    printf("%d\n",dis[f.x][f.y]+dis[t.x][t.y] + 1);                    return ;                }            }        }    }    cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;}int main(){    int t,sx,sy,ex,ey;    while(~scanf("%d%d",&n,&m))    {       for(int i = 0;i<n;i++)       {           scanf("%s",ma[i]);           for(int j = 0;j<m;j++)           {               if(ma[i][j] == 'a')               {                   sx = i; sy = j;               }               else if(ma[i][j]=='r')               {                   ex = i; ey = j;               }           }       }        BFS(sx,sy,ex,ey);    }    return 0;}


Slag

HDU 1242-rescue (bidirectional BFS) & amp; (BFS + 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.