hdu1026Ignatius and the Princess I

 

這題用的也是bfs,就是保存路徑這裏麻煩。。

#include<iostream>
#include<queue>
using namespace std;
#define max 99999999
int dir[4][2]={{0,-1},{0,1},{-1,0},{1,0}};
int flag;
int sum,n,m;
char map[105][105];
struct Node
{
    int x,y;
    int step;
};
struct Infor
{
   int prex;
   int prey;
   int time;
};
Infor path[105][105];
void bfs()
{
     int i,j,k;
     Node p,q;
     queue<Node>que;
     p.x=n-1;
     p.y=m-1;
     if(map[p.x][p.y]=='.')
        p.step=0;
     else
        p.step=map[p.x][p.y]-'0';
     que.push(p);//
     path[p.x][p.y].time=p.step;
     while(!que.empty())
     {
        q=que.front();
        que.pop();
        if(q.x==0&&q.y==0)//
        {
           if(path[q.x][q.y].time>q.step)
              path[q.x][q.y].time=q.step;
        }
        for(k=0;k<4;k++)
        {
           i=q.x+dir[k][0];
           j=q.y+dir[k][1];
           p.x=i;
           p.y=j;
           if(i<0||i>=n||j<0||j>=m)
              continue;
           if(map[i][j]=='X')
              continue;
           if(map[i][j]=='.')
           {
              if(q.step+1<path[i][j].time)
              {
                 p.step=path[i][j].time=q.step+1;
                 path[i][j].prex=q.x;
                 path[i][j].prey=q.y;
                 que.push(p);
              }
           }
           else
           {
               if(q.step+1+map[i][j]-'0'<path[i][j].time)
               {
                  p.step=path[i][j].time=q.step+1+map[i][j]-'0';
                  path[i][j].prex=q.x;
                  path[i][j].prey=q.y;
                  que.push(p);
               }
           }
        }
     }
}
 void Path(int i,int j,int t)
 {
      int x,y,z,p,q;
      if(map[i][j]>='1'&&map[i][j]<=9)
      {
         z=map[i][j]-'0';
         while(z--)
            printf("%ds:FIGHT AT (%d,%d)/n",t++,i,j);
      }
      while(!(i==n-1&&j==m-1))
      {
          if(map[i][j]>='1'&&map[i][j]<='9')
          {
             z=map[i][j]-'0';
             while(z--)
                   printf("%ds:FIGHT AT (%d,%d)/n",t++,i,j);
             printf("%ds:(%d,%d)->(%d,%d)/n",t++,i,j,path[i][j].prex,path[i][j].prey);
          }
          else
          {  
            printf("%ds:(%d,%d)->(%d,%d)/n",t++,i,j,path[i][j].prex,path[i][j].prey);
          }
           p = path[i][j].prex;
           q = path[i][j].prey;
           i = p;
           j = q;
      }
      if(map[i][j]>='1' && map[i][j] <='9')
      {
        z= map[i][j] - '0';           
        while(z--)
             printf("%ds:FIGHT AT (%d,%d)/n",t++,i,j);
      }
}
int main()
{
    int i,j;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
       for(i=0;i<n;i++)
         for(j=0;j<m;j++)
           cin>>map[i][j];
       if(map[0][0]=='X'||map[n-1][m-1]=='X')
       {
          cout<<"God please help our poor hero."<<endl;
          cout<<"FINISH"<<endl;
          continue;
       }
       for(i=0;i<n;i++)
          for(j=0;j<m;j++)
          {
            path[i][j].prex=i;
            path[i][j].prey=j;
            path[i][j].time=max;
          }
       bfs();
       if(path[0][0].time!=max)
       {
          printf("It takes %d seconds to reach the target position, let me show you the way./n",path[0][0].time);
          Path(0,0,1);//
       }
       else
           cout<<"God please help our poor hero."<<endl;
        cout<<"FINISH"<<endl;
    }
     return 0;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章