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

 

posted @ 2015-03-11 20:54  Run_For_Love  阅读(162)  评论(0编辑  收藏  举报