zoukankan      html  css  js  c++  java
  • hdu 1026 Ignatius and the Princess I (bfs打印路径)

    Problem Description
    The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pretty Princess. Now he gets into feng5166's castle. The castle is a large labyrinth. To make the problem simply, we assume the labyrinth is a N*M two-dimensional array which left-top corner is (0,0) and right-bottom corner is (N-1,M-1). Ignatius enters at (0,0), and the door to feng5166's room is at (N-1,M-1), that is our target. There are some monsters in the castle, if Ignatius meet them, he has to kill them. Here is some rules:
    1.Ignatius can only move in four directions(up, down, left, right), one step per second. A step is defined as follow: if current position is (x,y), after a step, Ignatius can only stand on (x-1,y), (x+1,y), (x,y-1) or (x,y+1). 2.The array is marked with some characters and numbers. We define them like this: . : The place where Ignatius can walk on. X : The place is a trap, Ignatius should not walk on it. n : Here is a monster with n HP(1<=n<=9), if Ignatius walk on it, it takes him n seconds to kill the monster.
    Your task is to give out the path which costs minimum seconds for Ignatius to reach target position. You may assume that the start position and the target position will never be a trap, and there will never be a monster at the start position.
     
    Input
    The input contains several test cases. Each test case starts with a line contains two numbers N and M(2<=N<=100,2<=M<=100) which indicate the size of the labyrinth. Then a N*M two-dimensional array follows, which describe the whole labyrinth. The input is terminated by the end of file. More details in the Sample Input.
     
    Output
    For each test case, you should output "God please help our poor hero." if Ignatius can't reach the target position, or you should output "It takes n seconds to reach the target position, let me show you the way."(n is the minimum seconds), and tell our hero the whole path. Output a line contains "FINISH" after each test case. If there are more than one path, any one is OK in this problem. More details in the Sample Output.
     
    Sample Input
    5 6
    .XX.1.
    ..X.2.
    2...X.
    ...XX.
    XXXXX.
    5 6
    .XX.1.
    ..X.2.
    2...X.
    ...XX.
    XXXXX1
    5 6
    .XX...
    ..XX1.
    2...X.
    ...XX.
    XXXXX.
     
    It takes 13 seconds to reach the target position, let me show you the way.
    1s:(0,0)->(1,0)
    2s:(1,0)->(1,1)
    3s:(1,1)->(2,1)
    4s:(2,1)->(2,2)
    5s:(2,2)->(2,3)
    6s:(2,3)->(1,3)
    7s:(1,3)->(1,4)
    8s:FIGHT AT (1,4)
    9s:FIGHT AT (1,4)
    10s:(1,4)->(1,5)
    11s:(1,5)->(2,5)
    12s:(2,5)->(3,5)
    13s:(3,5)->(4,5)
    FINISH
    It takes 14 seconds to reach the target position, let me show you the way.
    1s:(0,0)->(1,0)
    2s:(1,0)->(1,1)
    3s:(1,1)->(2,1)
    4s:(2,1)->(2,2)
    5s:(2,2)->(2,3)
    6s:(2,3)->(1,3)
    7s:(1,3)->(1,4)
    8s:FIGHT AT (1,4)
    9s:FIGHT AT (1,4)
    10s:(1,4)->(1,5)
    11s:(1,5)->(2,5)
    12s:(2,5)->(3,5)
    13s:(3,5)->(4,5)
    14s:FIGHT AT (4,5)
    FINISH
    God please help our poor hero.
    FINISH

         用bfs时,用优先队列来没一步,所以最先到达的一定是最短路径。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<queue>
     5 using namespace std;
     6 char mp[105][105];
     7 int map[105][105];
     8 int v[105][105];
     9 int ss[105][105];
    10 int yi[4][2]={{0,1},{1,0},{-1,0},{0,-1}};
    11 int n,m,temp;
    12 struct p
    13 {
    14     int x,y,t;
    15     friend bool operator<(p n1,p n2)
    16     {
    17         return n2.t<n1.t;
    18     }
    19 };
    20 p f,r;
    21 int pass(int x,int y)
    22 {
    23     if (x<0||x>=n||y<0||y>=m) return 0;
    24     if (map[x][y]==-1) return 0;
    25     return 1;
    26 }
    27 int bfs()
    28 {
    29      priority_queue<p>q;
    30      f.x=0;
    31      f.y=0;
    32      f.t=0;
    33      map[0][0]=-1;
    34      q.push(f);
    35      while (!q.empty())
    36      {
    37          r=q.top();
    38          q.pop();
    39         if (r.x==n-1&&r.y==m-1) return r.t;
    40          for (int i=0;i<4;i++)
    41          {
    42              f.x=r.x+yi[i][0];
    43              f.y=r.y+yi[i][1];
    44              if (pass(f.x,f.y))
    45              {
    46                  v[f.x][f.y]=i+1;
    47                  f.t=r.t+1+map[f.x][f.y];
    48                  map[f.x][f.y]=-1;
    49                  q.push(f);
    50              }
    51          }
    52      }
    53      return -1;
    54 }
    55 void prinln(int x,int y)
    56 {
    57     int rx,ry;
    58     if (v[x][y]==0) return ;
    59     rx=x-yi[v[x][y]-1][0];
    60     ry=y-yi[v[x][y]-1][1];
    61     prinln(rx,ry);
    62     printf("%ds:(%d,%d)->(%d,%d)
    ",temp++,rx,ry,x,y);
    63     while(ss[x][y]--)    printf("%ds:FIGHT AT (%d,%d)
    ",temp++,x,y);
    64 }
    65 int main()
    66 {
    67     int i,j;
    68     while (~scanf("%d%d",&n,&m))
    69     {
    70         memset(v,0,sizeof(v));
    71         memset(ss,0,sizeof(ss));
    72         for (i=0;i<n;i++)
    73         {
    74             for (j=0;j<m;j++)
    75             {
    76                 scanf(" %c",&mp[i][j]);
    77                 if (mp[i][j]=='.') map[i][j]=0;
    78                 else if (mp[i][j]=='X')  map[i][j]=-1;
    79                 else map[i][j]=ss[i][j]=mp[i][j]-'0';
    80             }
    81         }
    82         int ans=bfs();
    83         if (ans==-1) printf("God please help our poor hero.
    ");
    84         else
    85         {
    86             printf("It takes %d seconds to reach the target position, let me show you the way.
    ",ans);
    87             temp=1;
    88             prinln(n-1,m-1);
    89         }
    90         printf("FINISH
    ");
    91     }
    92 }
  • 相关阅读:
    教你50招提升ASP.NET性能(二十一):避免使用会话状态
    教你50招提升ASP.NET性能(二十):7条便利的ViewState技巧
    教你50招提升ASP.NET性能(二十):认识你的循环
    教你50招提升ASP.NET性能(十九):静态集合
    教你50招提升ASP.NET性能(十八):在处理网站性能问题前,首先验证问题是否出在客户端
    教你50招提升ASP.NET性能(十七):不要认为问题只会从业务层产生
    教你50招提升ASP.NET性能(十六):把问题仍给硬件而不是开发人员
    教你50招提升ASP.NET性能(十五):解决性能问题时不要低估UI的价值
    教你50招提升ASP.NET性能(十四):使用startMode属性来减少ASP.NET站点加载时间
    Chrome谷歌浏览器书签排序后,重启浏览器导致排序无效的问题(完美解决)
  • 原文地址:https://www.cnblogs.com/pblr/p/4883718.html
Copyright © 2011-2022 走看看