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 }
  • 相关阅读:
    超前进位加法器
    Xilinx ISE 12.3 LED测试程序
    位图文件(BMP)格式以及Linux下C程序实现
    SPB16.3安装系列指导
    Debian6显卡驱动安装
    深入浅出之正则表达式
    一个在台积电工作3年的工程师写给学弟学妹们的信
    Debian下解决MP3标题乱码
    ubuntu下安装usbblaster(更新)
    乘法器的Verilog HDL实现
  • 原文地址:https://www.cnblogs.com/pblr/p/4883718.html
Copyright © 2011-2022 走看看