Poj_1915
This question andPoj_2243It is very similar, but the size of the board changes. After reading the start and end points, you only need to start from the start point and8Perform extensive search at reachable locations and record the number of steps at the location in sequence. Exit the loop when the search ends.
# Include < Stdio. h >
# Include < String . H >
Int Vis [ 310 ] [ 310 ], DIS [ 310 ] [ 310 ], QX [ 90010 ], QY [ 90010 ];
Int DX [] = { 1 , 2 , 2 , 1 , - 1 , - 2 , - 2 , - 1 }, Dy [] = { 2 , 1 , - 1 , - 2 , - 2 , - 1 , 1 , 2 };
Int Main ()
{
Int I, J, K, L, N, front, rear, Sx, Sy, TX, Ty, X, Y, newx, newy;
Scanf ( " % D " , & N );
While (N -- )
{
Scanf ( " % D " , & L );
Scanf ( " % D " , & SX, & Sy );
Scanf ( " % D " , & TX, & Ty );
For (I = 0 ; I < L; I ++ )
For (J = 0 ; J < L; j ++ )
Vis [I] [J] = 0 ;
Front = Rear = 0 ;
QX [rear] = SX;
QY [rear] = Sy;
Rear ++ ;
Vis [SX] [sy] = 1 ;
Dis [SX] [sy] = 0 ;
While (Front < Rear)
{
X = QX [Front];
Y = QY [Front];
Front ++ ;
If (X = TX && Y = Ty)
Break ;
For (I = 0 ; I < 8 ; I ++ )
{
Newx = X + DX [I];
Newy = Y + Dy [I];
If ( ! Vis [newx] [newy] && Newx > = 0 && Newx < L && Newy > = 0 && Newy < L)
{
Vis [newx] [newy] = 1 ;
Dis [newx] [newy] = Dis [x] [Y] + 1 ;
QX [rear] = Newx;
QY [rear] = Newy;
Rear ++ ;
}
}
}
Printf ( " % D \ n " , DIS [TX] [ty]);
}
Return 0 ;
}