zoukankan      html  css  js  c++  java
  • hdu Rescue

    因为要求的是最少的时间,很明显的是一个利用优先队列的bfs的题目,题目很一般。

    #include"iostream"
    #include"algorithm"
    #include"stdio.h"
    #include"string.h"
    #include"cmath"
    #include"queue"
    #define mx 205
    using namespace std;
    int n,m,sx,sy,ex,ey;
    int dir[4][2]={{0,1},{0,-1},{-1,0},{1,0}};
    char map1[mx][mx];
    struct node
    {
        int x,y,steps;
        friend bool operator<(node a,node b)
        {
            return b.steps<a.steps;
        }
    };
    bool judge(int x,int y)
    {
        if(x>=0&&x<n&&y>=0&&y<m&&map1[x][y]!='#') return true;
        return false;
    }
    void bfs()
    {
        node cur,next;
        cur.x=sx;cur.y=sy;cur.steps=0;
        int i;
        priority_queue<node>q;
        q.push(cur);
        while(!q.empty())
        {
            cur=q.top();
            q.pop();
            if(cur.x==ex&&cur.y==ey){cout<<cur.steps<<endl;return;}
            for(i=0;i<4;i++)
            {
                next.x=cur.x+dir[i][0];
                next.y=cur.y+dir[i][1];
                if(judge(next.x,next.y))
                {
                    if(map1[next.x][next.y]=='x')
                    {
                        next.steps=cur.steps+2;
                    }
                    else next.steps=cur.steps+1;
                    map1[next.x][next.y]='#';
                    q.push(next);
                }
            }
        }
        cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
    }
    int main()
    {
        while(scanf("%d%d",&n,&m)==2)
        {
            int i,j;
            for(i=0;i<n;i++)
                for(j=0;j<m;j++)
            {
                cin>>map1[i][j];
                if(map1[i][j]=='r') {sx=i;sy=j;map1[i][j]='#';}
                else if(map1[i][j]=='a') {ex=i;ey=j;map1[i][j]='.';}
            }
            bfs();
        }
        return 0;
    }
    View Code
  • 相关阅读:
    实现IEnumberable接口和IEnumberator
    XAML-1
    Java基础00-Java概述1
    详解Lombok中的@Builder用法
    stream之map的用法
    stream之forEach的用法
    Java中map.getOrDefault()方法的使用
    BiPredicate的test()方法
    Function.identity()
    java 8 lamda Stream的Collectors.toMap 参数
  • 原文地址:https://www.cnblogs.com/acm-jing/p/4330933.html
Copyright © 2011-2022 走看看