HDU 3638 go, Susu

Source: Internet
Author: User

Hdu_3638

This question is not much different from that of a common BFS in the maze. Therefore, you may wish to separate the monster from each other and process the view of the monster every second, then, this second will not go to the monster's field of view, but will not go to the * position.

When dealing with the view of a monster, you can first use a struct to store the monster's location, and automatically update the monster's location every second, create a table with four directions and nine relative positions in each direction, which is advantageous for the view of the monster.

# Include <stdio. h> # Include < String . H> # Include <Algorithm> # Include <Queue> # Define Maxn 60 Int Trans [] = {0 , 2 , 1 , 4 , 3  };  Int DX [] = { 0 ,- 1 , 1 , 0 , 0 }, Dy [] = { 0 , 0 ,0 ,- 1 , 1  };  Int Ex [] [ 9 ] = {{  0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,0  },{  0 ,- 1 ,- 1 ,- 1 ,- 2 ,- 2 ,- 2 ,- 2 ,- 2  },{  0 , 1 , 1 ,1 , 2 , 2 , 2 , 2 , 2  },{  0 ,- 1 , 0 , 1 ,- 2 ,- 1 , 0 , 1 , 2  },{  0 ,- 1 , 0 , 1 ,- 2 ,- 1 , 0 , 1 , 2  }};  Int Ey [] [ 9 ] = {{ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0  },{  0 ,- 1 , 0 , 1 ,- 2 ,- 1 , 0 , 1 , 2  },{  0 ,- 1 , 0 , 1 ,- 2 ,- 1 , 0 , 1 , 2  },{ 0 ,- 1 ,- 1 ,- 1 ,- 2 ,- 2 ,- 2 ,- 2 ,- 2  },{  0 , 1 , 1 , 1 , 2 , 2 , 2 , 2 , 2  }};  Struct  Monster {  Int  X, Y, D;} mon [maxn];  Char  B [maxn];  Int  N, m, K, Sx, Sy, TX, Ty, G [maxn] [maxn], del [maxn] [maxn], vis [maxn] [maxn];  Struct  Point { Int  X, Y, T; point () {} Point (  Int _ X, Int _ Y, Int  _ T): X (_ x), y (_ y), T (_ t) {}}; inline  Int Inside ( Int X, Int  Y ){  Return X> = 1 & X <= N & Y> = 1 & Y <=M ;}  Void  Moveall (){  Int  I, X, Y;  For (I = 0 ; I <K; I ++ ) {X = Mon [I]. x + dx [Mon [I]. d], y = mon [I]. Y + Dy [Mon [I]. d];  If (Inside (x, y) & G [x] [Y]) Mon [I]. x = x, mon [I]. Y = Y;  Else Mon [I]. d = Trans [Mon [I]. d];} Void  Delblock (){  Int  I, j, X, Y;  For (I = 1 ; I <= N; I ++ )  For (J = 1 ; J <= m; j ++) del [I] [J] = 1 - G [I] [J];  For (I = 0 ; I <K; I ++ ) For (J = 0 ; J < 9 ; J ++ ) {X = Mon [I]. x + ex [Mon [I]. d] [J], y = mon [I]. Y + Ey [Mon [I]. d] [J];  If (Inside (x, y) del [x] [Y] = 1  ;}}  Void  Init (){  Int  I, J; scanf (  "  % D " , & N ,& M );  For (I = 1 ; I <= N; I ++ ) {Scanf (  "  % S  " , B + 1  );  For (J = 1 ; J <= m; j ++ ) {G [I] [J] = B [J]! = ' *  '  ;  If (B [J] = '  A  ' ) SX = I, Sy = J;  Else   If (B [J] = '  B  ' ) Tx = I, Ty = J ;}} scanf (  "  % D " ,& K );  For (I = 0 ; I <K; I ++) scanf ( "  % D  " , & Mon [I]. X, & mon [I]. Y ,& Mon [I]. d );}  Void  Solve (){  Int I, j, X, Y, cur, ANS =- 1  ; Cur =- 1 ; Delblock ();  If  (DEL [SX] [sy]) {printf (  "  The victory and defeat of the military are not coming back soon.  "  );  Return  ;} STD: queue <Point> Q; q. Push (point (sx, Sy,  0  ));  While (! Q. Empty () {point P =Q. Front (); q. Pop ();  If (P. T> 1000 ) Break  ;  If (P. x = TX & P. Y = Ty) {ans = P. t;  Break  ;}  If (P. T> Cur) {cur = P. t; For (I = 1 ; I <= N; I ++ )  For (J = 1 ; J <= m; j ++) vis [I] [J] = 0  ; Moveall (), delblock ();}  For (I = 0 ; I < 5 ; I ++ ) {X = P. x + dx [I], y = P. Y + Dy [I]; If (Inside (x, y )&&! Del [x] [Y] &! Vis [x] [Y]) vis [x] [Y] = 1 , Q. Push (point (X, Y, P. t + 1  ));}}  If (ANS =- 1 ) Printf ( "  The victory and defeat of the military are not coming back soon.  "  );  Else Printf ( " % D \ n  "  , ANS );}  Int  Main (){  Int  T, TT; scanf (  "  % D  " ,& T );  For (Tt = 1 ; TT <= T; TT ++ ) {Init (); printf (  " Case % d:  "  , TT); solve ();}  Return   0  ;} 
Related Article

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.