This is a pure simulated question. You only need to simulate it carefully. At the beginning, you changed the row-column relationship according to your own ideas, and the results were in disorder. After this certificate, the AC.
CodeAs follows:
# Include <cstring> # Include <Cstdio> # Include <Cstdlib> # Define Maxn 105 Using Namespace STD; Int A, B, n, m, X [maxn], Y [maxn], d [maxn]; Int G [maxn] [maxn]; Int Dir [4 ] [ 2 ] = {- 1 , 0 , 0 , 1 , 1 , 0 , 0 ,- 1 }; Char Box [maxn]; // Accept information Struct Message { Int No, times; Char OP [ 3 ];} M; Bool Update ( Int & Who, Int & Kind ){ If (M. Op [ 0 ] = ' L ' ){// Left turn D [M. No] = (d [M. No]-M. Times + 120 ) % 4 ;} Else If (M. Op [ 0 ] = ' R ' ){ // Right D [M. No] = (d [M. No] + M. Times) % 4 ;} Else { // Forward While (M. times -- ) {G [x [M. No] [Y [M. No] = 0 ; Int Xx = (X [M. No] + = dir [d [M. No] [ 0 ]); Int YY = (Y [M. No] + = dir [d [M. No] [ 1 ]); If (Xx < 1 | XX> A | YY < 1 | YY> B) {kind = 1 ; Return True ;} Else If (G [XX] [YY]! = 0 ) {Kind = 2 ; WHO = G [XX] [YY]; Return True ;} Else {G [XX] [YY] = M. No ;}}} Return False ;} Int Main (){ Int T; Bool Over; Char Temp [ 3 ]; Scanf ( " % D " ,& T ); While (T -- ) {Memset (G, 0 , Sizeof (G); Box [ 0 ] =' O ' , Box [ 1 ] = ' K ' , Box [ 2 ] = ' \ 0 ' ; Over = False ; Scanf ( " % D " , & A, & B ); // A is wide, B is high Scanf ( " % D " , & N, & M ); // N indicates the number of robots, and M indicates the number of commands. For ( Int I = 1 ; I <= N; ++ I) {scanf ( " % D % s " , & X [I], & Y [I], temp); G [x [I] [Y [I] = I; Switch (Temp [ 0 ]) { Case ' W ' : D [I] = 0 ; Break ; Case ' N ' : D [I] = 1 ; Break ; Case ' E ' : D [I] = 2 ; Break ; Case ' S ' : D [I] =3 ; Break ;}} While (M -- ){ Int Who, kind; scanf ( " % D % S % d " , & M. No, M. Op ,& M. Times ); If (Over ){ Continue ;} If (Update (WHO, kind) {over = True ; If (Kind = 1 ) {Sprintf (box, " Robot % d crashes into the wall " , M. No );} Else {Sprintf (box, " Robot % d crashes into robot % d " , M. No, WHO) ;}} puts (box );} Return 0 ;}