HDU--1026:Ignatius and the Princess I (BFS+优先队列+打印路径)

题目源地址:http://acm.hdu.edu.cn/showproblem.php?pid=1026


//HOJ--1026:Ignatius and the Princess I
#include
#include
#include
#include
using namespace std;

struct Coordinate
{
   int x,y;
   int step;
   //因为存在怪兽而在原地打死怪兽也需要时间,若不使用优先队列则可能两条路径上下左右走过的路径一样长,
   //而打怪兽的时间不一样,使用优先队列保证了时间的最短 
   friend bool operator<(Coordinate c1,Coordinate c2)  
   {  
        return c2.step Q; 
    Q.push(start);
    visited[start.x][start.y]=1;
    
    while(!Q.empty())
    {
       temp=Q.top();
       Q.pop();
       
       if(temp.x==N-1 && temp.y==M-1)
             return temp.step;
             
       for(int i=0;i<4;i++)
       {
          next.x=temp.x+dx[i];
          next.y=temp.y+dy[i];
          
          if(next.x>=0 && next.x=0 && next.y("<("
        <>N>>M)
    {
       getchar();
       memset(visited,0,sizeof(visited));
       
       for(i=0;i>map[i][j];
             
       start.x=0;
       start.y=0;
       start.step=0;
       
       path[0][0].x=-1;  //起始点无前驱节点
       path[0][0].y=-1;
       path[0][0].time=0; 
       
       ans=BFS();
       if(ans)
       {
          cout<<"It takes "<


你可能感兴趣的:(ACM解题报告)