HDU 1242 (BFS search + priority queue)

Source: Internet
Author: User

Question Link: Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1242

question .

solution :

If there is no special point, it is common BFs.

Because of the intervention of special points, a point may arrive in different ways, so the priority queue is used. For a point, the method used to retrieve the first point must be optimal.

At the same time, the use of priority queue also provides a pruning for the BFS process, because the first arrival is certainly the best result. As long as the first result is found, no search is needed.

Note that there are multiple start points, so first record all the start points and find the minimum BFS in sequence.

 

#include "cstdio"#include "string"#include "cstring"#include "iostream"#include "vector"#include "queue"using namespace std;#define inf 1<<28struct status{    int x,y,dep;    status(int x,int y,int dep):x(x),y(y),dep(dep) {}    bool operator < (const status &a) const    {        return dep>a.dep;    }};int n,m,map[205][205],vis[205][205],dir[4][2]={-1,0,1,0,0,-1,0,1},ans;void bfs(int x,int y){    priority_queue<status> Q;    Q.push(status(x,y,0));    bool flag=false;    while(!Q.empty())    {        if(flag) break;        status t=Q.top();Q.pop();        vis[t.x][t.y]=true;        for(int s=0;s<4;s++)        {            int X=t.x+dir[s][0],Y=t.y+dir[s][1];            if(vis[X][Y]||X<0||X>n||Y<0||Y>n||!map[X][Y]) continue;            if(map[X][Y]==2) {flag=true;ans=min(ans,t.dep+1);};            if(map[X][Y]==3) Q.push(status(X,Y,t.dep+2));            else Q.push(status(X,Y,t.dep+1));        }    }}int main(){    //freopen("in.txt","r",stdin);    ios::sync_with_stdio(false);    string tt;    while(cin>>n>>m&&n)    {        vector<status> f;        ans=inf;        for(int i=1;i<=n;i++)        {            cin>>tt;            for(int j=0;j<tt.size();j++)            {                if(tt[j]==‘#‘) map[i][j+1]=0;                if(tt[j]==‘.‘) map[i][j+1]=1;                if(tt[j]==‘a‘) map[i][j+1]=2;                if(tt[j]==‘x‘) map[i][j+1]=3;                if(tt[j]==‘r‘) {map[i][j+1]=4;f.push_back(status(i,j+1,0));}            }        }        for(int i=0;i<f.size();i++)        {            memset(vis,0,sizeof(vis));            bfs(f[i].x,f[i].y);        }        if(ans!=inf) cout<<ans<<endl;        else cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;    }}

 

11867790 00:53:01 Accepted 1242 46 Ms 644 K 1840 B C ++ Physcal

 

 

 

HDU 1242 (BFS 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.