hdoj 1026 Ignatius and the Princess I

题目网站 :http://acm.hdu.edu.cn/showproblem.php?pid=1026

 

 

这题就是求从 (0,0)->(n-1,m-1)的最短路径,算法早就有了,可是自己写的还是很不顺,于是 baidu了一下,参考了别人的写法,但是意思都一样

 

下面的算法是 从 (n-1,m-1)到(0,0)的来算最短路径的

 

代码和注释:

 

#include<iostream>
#include<queue>
using namespace std;
int mark[100][100],fight[100][100]; //标记是否走过   (i,j)处的战斗时间
int n,m;
typedef struct
{
  int x,y; //从(0,0)到(n-1,m-1)过程中下一步的坐标
  char c;
}Node;
Node maze[100][100];

int dir[][2]={{-1,0},{1,0},{0,1},{0,-1}};//四个方向

typedef struct node
{
 int x,y;
 int time;//时间
 bool operator<(const node t) const
 {
  return time>t.time;
 }
}node;

node now,next;

priority_queue<node> que;

void init() //清空队列
{
   while(!que.empty())
     que.pop();
      
}
void input() //数据的输入
{
  for(int i=0;i<n;i++)
  {
     for(int j=0;j<m;j++)
       {
         scanf("%c",&maze[i][j].c);
         if(maze[i][j].c>='1'&&maze[i][j].c<='9')
           fight[i][j]=maze[i][j].c-'0';
          else
           fight[i][j]=0;
         mark[i][j]=0;     
       }
    getchar();    
  }
}
int bfs()
{
  int i;
  now.x=n-1;
  now.y=m-1;
  if(maze[now.x][now.y].c>='1'&&maze[now.x][now.y].c<='9'){//(n-1.m-1)有怪兽
      now.time=maze[now.x][now.y].c-'0';
      fight[now.x][now.y]=maze[now.x][now.y].c-'0';}
  else
      now.time=0;  //没怪兽
   mark[now.x][now.y]=1; //访问过了
  que.push(now);
 
  while(!que.empty())
   {
     now=que.top();
     que.pop();
     if(now.x==0&&now.y==0)
       return now.time;
     for(i=0;i<4;i++)
      {
        next.x=now.x+dir[i][0];
        next.y=now.y+dir[i][1];
        if(next.x>=0 && next.x<n && next.y>=0 && next.y<m && maze[next.x][next.y].c!='X' && !mark[next.x][next.y])
          //满足条件的(next.x,next.y)

          {
              if(maze[next.x][next.y].c>='1'&&maze[next.x][next.y].c<='9'){
                   next.time=now.time+maze[next.x][next.y].c-'0'+1;
                   fight[next.x][next.y]=maze[next.x][next.y].c-'0';}
               else
                   next.time=now.time+1;
       
               mark[next.x][next.y]=1;   
               maze[next.x][next.y].x=now.x;
               maze[next.x][next.y].y=now.y;
               que.push(next);                                                                                                       
          }
      }                   
   }
   return 0; //队列为空时,返回0
}
void output()
{
  int i;
  int t=bfs();
  if(t)
   {
     printf("It takes %d seconds to reach the target position, let me show you the way./n",t); 
      int sec=1,x=0,y=0,tx,ty;
      while(sec!=t+1)
       {
         printf("%ds:(%d,%d)->(%d,%d)/n",sec++,x,y,maze[x][y].x,maze[x][y].y);
         for(i=1;i<=fight[maze[x][y].x][maze[x][y].y];i++)
            printf("%ds:FIGHT AT (%d,%d)/n",sec++,maze[x][y].x,maze[x][y].y);
        tx=maze[x][y].x;ty=maze[x][y].y;
        x=tx;
        y=ty;
       }
   }
   else
   {
     printf("God please help our poor hero./n");    
   }   
   printf("FINISH/n"); 
}
int main()
{  
    while(scanf("%d%d",&n,&m)!=EOF)
      { getchar();
        init();
        input();
        output();           
      }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值