hdu 1026 Ignatius and the Princess (BFS + path print)

Original link: http://www.cnblogs.com/heat-man/archive/2013/04/09/3011008.html

Problem link adress: http://acm.hdu.edu.cn/showproblem.php?pid=1026

The subject has long met, but because there is no print BFS path of experience is put today! The following first to write printing method for recording precursor path.

This article describes only part of the print path, BFS to slightly up.

Implementation process: from the inlet to the outlet of the search, and before a position information of each position (i.e., the current location and previous location link) record, so that wear has become a path from beginning to end.

Search such as from 1 to 10, we can consider the search from 10 to 1 to 9 when the search time, a position before recording, i.e. at 9 10, This in turn forward until 1;

Then can be output when first output 1, and output the previous marker 1, i.e. 2, and then outputs a marker 2 before 3, i.e., up to 10, such a full path is printed out.

View Code
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<queue>
 5 using namespace std;
 6 #define N 105
 7 struct node{
 8     friend bool operator<(node a,node b)
 9     {
10         return a.time>b.time;
11     };
12     int x,y,time;
13 };
14 
15 struct A{
16     int x,y;
17 }s[N][N];//记录map[i][j]前一步的位置
18 
19 int n,m;
20 Mark int [N] [N];
 21 is  char Map [N] [N];
 22 is  int the dir [. 4] [2] = { 0,1,0, -1,1,0, -1,0 } ;
 23 is  the BFS void ()
 24 {
 25      Node CUR, Next;
 26 is      Memset (Mark, 0, the sizeof (Mark));
 27      int X, Y, I;
 28      The priority_queue <Node> Q;
 29      cur.x-n-=. 1; CUR = m. 1-.y; cur.time = 0;
 30      S [n--. 1] [-m. 1] .x = -1; // tag junction
 31 is      mark [. 1-n-] [. 1-m] =. 1;
 32      (! '.' Map [. 1-n-] [. 1-m] =) IF // Note that if there is a monster outlet
 33 is          cur.time = Map [. 1-n-] [. 1-m] - '0';
 34 is      q.push (CUR);
 35     the while (q.empty ()!)
 36      {
 37 [          CUR = q.top ();
 38 is          q.pop ();
 39          IF (cur.x == 0 && cur.y == 0) // use herein is from the outlet Therefore, if the inlet traverse to the method outputs the paths traversed inlet
 40          {
 41 is              the printf ( "% D it Takes to seconds the REACH the target position, the let you the Show Me Way \ n-.", cur.time);
 42 is              int = K . 1;
 43 is              int = A cur.x; // A, B represents the current position coordinates
 44 is              int = B cur.y;
 45              the while (! S [A] [B] .x = -. 1) already intentionally // tag used herein to pop off cycling conditions
 46 is              {
 47                  int C = S [a] [B] .x; // C, D represents the current position coordinates of the previous
 48                 int D = S [A] [B] .y;
 49                  the printf ( "% DS : (% D,% D) -> (% D,% D) \ n-", K ++, A, B, C, D) ;
 50                  IF (! '.' Map [C] [D] =) // if strange, Daguai time output
 51 is                  {
 52 is                      for (int KK = 0; KK <Map [C] [D] - '0' ; KK ++)
 53 is                          the printf ( "% DS : FIGHT the AT (% D,% D) \ n-", K ++, C, D) ;
 54 is                  }
 55                  A = C; B = D;
 56 is              }
 57 is              the printf ( "FINISH \ n- ");
 58              return;
 59          }
 60          
61 is      
62 is     for(i=0;i<4;i++)
63     {
64         next.x=x=cur.x+dir[i][0];
65         next.y=y=cur.y+dir[i][1];
66         if(x>=0&&x<n&&y>=0&&y<m&&mark[x][y]==0&&map[x][y]!='X')
67         {
68             if(map[x][y]=='.')
69                 next.time=cur.time+1;
70             else
71                 next.time=cur.time+map[x][y]-'0'+1;
72             q.push(next);
73             mark[x][y]=1;
74             s[x][y].x=cur.x;//记录当前位置的前一个位置
75             s[x][y].y=cur.y;
76         }
77     }
78     }
79 
80     printf("God please help our poor hero.\nFINISH\n");
81 }
82 
83 
84 int main()
85 {
86     while(scanf("%d%d",&n,&m)!=EOF)
87     {
88         getchar();
89         int i;
90         memset(s,0,sizeof(s));
91        
92         for(i=0;i<n;i++)
93             cin>>map[i];
94         BFS();
95     }
96     return 0;
97 }

 

Reproduced in: https: //www.cnblogs.com/heat-man/archive/2013/04/09/3011008.html

Guess you like

Origin blog.csdn.net/weixin_30617561/article/details/95128494