zoukankan      html  css  js  c++  java
  • zoj 1649 Rescue

    刚开始按一般搜索写一直错,没有想明白怎么回事。搜了一下其他人的思路才明白,本题有杀死守卫的时间,所以bfs搜索出来路径最短的不一定是时间最短的,所以使用了优先队列让他自己在队列里面排序解决。还有自己刚开始还将路变成了墙,现在想来也是错的,主要都是要杀人。
    或者考虑一直搜索,每次将结果与答案比较,取最小的保留即可,数据比较小,可以这样完成。
    优先队列版本

    #include<stdio.h>
    #include<queue>
    #include<algorithm>
    #include<string.h>
    #include<iostream>
    using namespace std;
    
    struct node
    {
        int x,y,t;
         bool operator <(const node a) const
        {
           // if(t==a.t) return a.d<d;
            return t>a.t;
        }
    }p1,p;
    
    int dx[]={1,-1,0,0};
    int dy[]={0,0,1,-1};
    int n,m,ans,tme[200+5][200+5];
    char mp[2000+5][2000+5];
    priority_queue<node>q;
    
    int bfs(int sx,int sy,int ex,int ey)
    {
        while(!q.empty()) q.pop();
        p1.t=0;
        p1.x=sx;
        p1.y=sy;
        tme[sx][sy]=0;
        mp[p1.x][p1.y]='#';
        q.push(p1);
        while(!q.empty())
        {
            p1=q.top();
            q.pop();
            if(p1.x==ex&&p1.y==ey) {return p1.t;}
            for(int i=0;i<4;i++)
            {
                p.x=p1.x+dx[i];
                p.y=p1.y+dy[i];
                p.t=p1.t+1;
                if(p.x<0||p.x>=n||p.y<0||p.y>=m||mp[p.x][p.y]=='#') continue;
                if(mp[p.x][p.y]=='x') p.t++;
                if(tme[p.x][p.y]>p.t)
                {
                    tme[p.x][p.y]=p.t;
                    q.push(p);
                }
            }
        }
        return 0;
    }
    int main()
    {
        int i,j,sx,sy,ex,ey;
        while(~scanf("%d%d",&n,&m))
        {
            memset(mp,0,sizeof(mp));
            memset(tme,9999,sizeof(tme));
            for(i=0;i<n;i++)
                scanf("%s",mp[i]);
    
            for(i=0;i<n;i++)
            {
                for(j=0;j<m;j++)
                {
                    if(mp[i][j]=='r')
                    {
                        sx=i;
                        sy=j;
                    }
                    else if(mp[i][j]=='a')
                    {
                        ex=i;
                        ey=j;
                    }
                }
            }
            ans=0;
            ans=bfs(sx,sy,ex,ey);
            if(!ans) printf("Poor ANGEL has to stay in the prison all his life.
    ");
            else printf("%d
    ",ans);
        }
        return 0;
    }

    法二

    #include<stdio.h>
    #include<queue>
    #include<algorithm>
    #include<string.h>
    #include<iostream>
    using namespace std;
    
    struct node
    {
        int x,y,t;
    } p1,p;
    int dx[]= {1,-1,0,0};
    int dy[]= {0,0,1,-1};
    int n,m,ans,tme[200+5][200+5];
    char mp[200+5][200+5];
    queue<node>q;
    
    void bfs(int sx,int sy,int ex,int ey)
    {
        while(!q.empty()) q.pop();
        p1.t=0;
        p1.x=sx;
        p1.y=sy;
        tme[sx][sy]=0;
        //mp[p1.x][p1.y]='r';
        q.push(p1);
        while(!q.empty())
        {
            p1=q.front();
            q.pop();
            if(p1.x==ex&&p1.y==ey)
            {
                //printf("%d
    ",p1.t);
                if(p1.t<ans) ans=p1.t;
            }
            for(int i=0; i<4; i++)
            {
                p.x=p1.x+dx[i];
                p.y=p1.y+dy[i];
                p.t=p1.t+1;
                if(p.x<0||p.x>=n||p.y<0||p.y>=m||mp[p.x][p.y]=='#') continue;
                if(mp[p.x][p.y]=='x') p.t++;
                if(tme[p.x][p.y]>p.t)
                {
                    tme[p.x][p.y]=p.t;
                    q.push(p);
                }
            }
        }
    }
    int main()
    {
        int i,j,sx,sy,ex,ey;
        while(~scanf("%d%d",&n,&m))
        {
            memset(mp,0,sizeof(mp));
            memset(tme,9999,sizeof(tme));
            for(i=0; i<n; i++)
                scanf("%s",mp[i]);
    
            for(i=0; i<n; i++)
            {
                for(j=0; j<m; j++)
                {
                    if(mp[i][j]=='r')
                    {
                        sx=i;
                        sy=j;
                    }
                    else if(mp[i][j]=='a')
                    {
                        ex=i;
                        ey=j;
                    }
                }
            }
            ans=9999;
            bfs(sx,sy,ex,ey);
            if(ans==9999) printf("Poor ANGEL has to stay in the prison all his life.
    ");
            else printf("%d
    ",ans);
        }
        return 0;
    }
    

    版权声明:本文为博主原创文章,未经博主允许不得转载。http://xiang578.top/

  • 相关阅读:
    构建之法 阅读笔记04
    团队项目-个人博客6.6
    个人总结
    第十六周学习进度条
    第十五周学习进度条
    构建之法阅读笔记07
    大道至简阅读笔记03
    大道至简阅读笔记02
    大道至简阅读笔记01
    第十四周学习进度条
  • 原文地址:https://www.cnblogs.com/xryz/p/4848020.html
Copyright © 2011-2022 走看看