Hdu2531catch him (extensive search BFS)

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php? PID = 2531

Problem description in American football, the player is responsible for directing the entire team's offensive tactics and runtimes, as well as the task of passing the ball to the players. As the most important player in a team's attack group, the player is usually relatively weak in the body. Therefore, the team usually arranges five to seven Chinese experts to protect him, among them, the five players standing in front of the defender and lined up in the front line are known as the attacking front lines. They are usually about 135 of strong men. For the defender, attacking the opponent's defender is of course the most direct method to restrict the opponent's attack. If the effect is good, you can push the opponent to the ground before the opponent passes the ball, which is called killing. Kill is the best way to boost the morale of the defensive team. Because the opponent has no chance to pass the ball, the attack is over and the distance between the two sides must be regressed. The fierce kill can even hurt the opponent's defender, forcing the opponent to change the core of the attack. In this question, enter the positions of the Defender preparing to kill the dashboard, the positions of each opponent's offensive front-line player, and the positions of the opponent's dashboard, your task is to find at least how many steps the Defender is about to move to prepare for the kill to kill the opponent's defender. Assume that the opponent's attack front line and the defender won't move in this process. There is only one defender, and the defender will only kill when it hits the opponent's defender. All players are in a continuous, non-empty two-dimensional area. The defender cannot pass through the body of the attacking front line or the outside (only through the open space ). The defender cannot turn the body, but can only translate. The process of moving all parts of the Defender's body in the same direction (top, bottom, left, right) is called step 1. The input contains multiple groups of data. The first row of each group of data is an integer h, w (0 Algorithm C ++ Code As follows:
 1 # Include <iostream> 2 # Include <queue> 3   Using   Namespace  STD;  4   Char Cmap [ 110 ] [ 110 ]; //  Define Map  5  Int Mark [ 110 ] [ 110 ]; //  Define tag  6   Struct  Node  7   {  8       Int  X, Y, step;  9 } D, P; //  Nodes. Note that only one of the nodes is saved. 10   Int BX [ 30 ], [ 30 ]; //  Array with relative locations  11   Int CX [] = { 1 ,- 1 , 0 , 0  };  12   Int CY [] = {0 , 0 ,- 1 , 1  };  13   Int BFS ( Int Len, Int M, Int N, Int X, Int  Y)  14   {  15      Int  I, J;  16 Queue <node> Q;  17 Memset (mark, 0 , Sizeof  (Mark ));  18 D. x = X;  19 D. Y = Y;  20 D. Step = 0  ; 21   Q. Push (d );  22       For (I = 0 ; I <Len; I ++ )  23 Mark [BX [I] [by [I] = I + 1 ; //  Mark with location  24       While (! Q. Empty ())  25   { 26 D = Q. Front ();  27   Q. Pop ();  28           For (I = 0 ; I <Len; I ++ )  29               If (Cmap [D. x + bx [I] [D. Y + by [I] = '  Q  '  )  30                   Return D. step;  31           For (I = 0 ; I < 4 ; I ++ )  32   {  33 P. x = D. x + CX [I];  34 P. Y = D. Y + CY [I];  35 P. Step = D. Step + 1  ; 36   37               //  Conditional judgment  38               For (J = 0 ; J <Len; j ++ )  39   {  40                   Int  TX, Ty;  41 Tx = p. x + BX [J];  42 Ty = P. Y +By [J];  43                   If (Cmap [TX] [ty] = '  O  ' | TX < 0 | TX> = M | ty < 0 | Ty> = n | Mark [TX] [ty] = J + 1  )  44                       Break  ;  45   }  46              If (J < Len)  47                   Continue ; //  If the break comes out, the push and tag will not be entered.  48   Q. Push (P );  49   50               //  Mark  51               For (J = 0 ; J <Len; j ++ ) 52   {  53                   Int  TX, Ty;  54 Tx = p. x + BX [J];  55 Ty = P. Y + By [J];  56 Mark [TX] [ty] = J + 1  ;  57   }  58  }  59   }  60       Return   0  ;  61   }  62   Int  Main ()  63   {  64       Int  M, N;  65      While (~ Scanf ( "  % D  " , & M ,& N ))  66   {  67           If (! M &&! N)  68               Break  ;  69           Int  I, J, K;  70          For (I = 0 ; I <m; I ++ )  71 Scanf ( "  % S  "  , Cmap [I]);  72           For (K = I = 0 ; I <m; I ++ )  73               For (J = 0 ; J <n; j ++)  74   {  75                   If (Cmap [I] [J] = '  D  '  )  76   {  77 BX [k] = I;  78 By [k] = J;  79 K ++ ;  80   }  81   }  82           Int SX = Bx [ 0 ], Sy = [ 0  ];  83           For (I = 0 ; I <K; I ++) //  Generate relative position Array 84   {  85 BX [I]-= SX;  86 By [I]-= Sy;  87   }  88           Int Ans = BFS (K, M, N, Sx, Sy );  89           If  (ANS)  90 Printf ("  % D \ n  "  , ANS );  91           Else  92 Puts ( "  Impossible  "  );  93   }  94       Return   0  ; 95 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.