码迷,mamicode.com
首页 > 其他好文 > 详细

hdu Rescue

时间:2015-03-11 21:19:06      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

因为要求的是最少的时间,很明显的是一个利用优先队列的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

 

hdu Rescue

标签:

原文地址:http://www.cnblogs.com/acm-jing/p/4330933.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!