[HDU] 1242 Rescue 不使用优先队列来找寻最短路径

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1242

方法:将几个r去找一个a的哪个r先找到a 的问题转换成 一个a去找多个r看先找到哪个r问题,可以使用优先队列,建模成一个单源最短路径来解答。但是本程序实现中没有用优先队列,而是就一班的广搜,由于有多个目标,题目要求找到到目标最快的一个目标,所以就在广搜到每一个目标的每一个时候,用达到其的时间用比较的方式去更新当前维护的一个最小到达时间。

感想:要换角度思考问题.

View Code
#include <iostream>
#include <queue>
using namespace std;
int rowsCount,columsCount;
char landGrids[205][205];
bool visited[205][205];
char guide = 'x';
char firend = 'r';
char angel = 'a';
char wall='#';
struct point
{
    int x,y;
    int currentStep;
};
bool canGo(int x,int y)
{
    if(x<1 || y<1 ||x>rowsCount || y>columsCount )
        return false;
    if(landGrids[x][y] == wall)
        return false;
    return !visited[x][y];
}
int search(point* st,int minFoundStep )
{
    queue<point*> record;
    record.push(st);     
    while(!record.empty())
    {
        point* tempPoint = record.front();
        if( landGrids[tempPoint->x][tempPoint->y] == firend)
        {
            if(minFoundStep>tempPoint->currentStep)
                minFoundStep = tempPoint->currentStep;
        }
        else
        {
            if(tempPoint->currentStep<minFoundStep)
            {
                if(canGo(tempPoint->x+1,tempPoint->y))
                {
                    point* down = (point*)(malloc(sizeof(point)));
                    down->x = tempPoint->x+1;
                    down->y = tempPoint->y;
                    down->currentStep=tempPoint->currentStep+(landGrids[tempPoint->x][tempPoint->y]==guide ? 2 :1);
                    record.push(down);
                }
                if(canGo(tempPoint->x-1,tempPoint->y))
                {
                    point* up = (point*)(malloc(sizeof(point)));
                    up->x = tempPoint->x-1;
                    up->y = tempPoint->y;
                    up->currentStep=tempPoint->currentStep+(landGrids[tempPoint->x][tempPoint->y]==guide ? 2 :1);
                    record.push(up);
                }
                if(canGo(tempPoint->x,tempPoint->y+1))
                {
                    point* right = (point*)(malloc(sizeof(point)));
                    right->x = tempPoint->x;
                    right->y = tempPoint->y+1;
                    right->currentStep=tempPoint->currentStep+(landGrids[tempPoint->x][tempPoint->y]==guide ? 2 :1);
                    record.push(right);
                }
                if(canGo(tempPoint->x,tempPoint->y-1))
                {
                    point* left = (point*)(malloc(sizeof(point)));
                    left->x = tempPoint->x;
                    left->y = tempPoint->y-1;
                    left->currentStep=tempPoint->currentStep+(landGrids[tempPoint->x][tempPoint->y]==guide ? 2 :1);
                    record.push(left);
                }
            }
        }
        visited[tempPoint->x][tempPoint->y]=true;
        record.pop();
    }
    return minFoundStep;
}
void main()
{
    while(scanf("%d %d",&rowsCount,&columsCount) != EOF)
    {
        point* startPoint;
        int result=0;
         for(int i=1;i<=rowsCount;i++)
             for(int j=1;j<=columsCount;j++)
             {
                cin>>landGrids[i][j];
                visited[i][j]=false;
                if(landGrids[i][j]==angel)
                {
                    startPoint = (point*)(malloc(sizeof(point)));
                    startPoint->x = i;
                    startPoint->y = j;
                    startPoint->currentStep = 0;
                }
             }
             result = search(startPoint,rowsCount*columsCount+1);
             if(result == rowsCount*columsCount+1)
                 cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
             else
                cout<<result<<endl;
    }
}
原文地址:https://www.cnblogs.com/kbyd/p/3024011.html