Poj_2243
After reading and converting 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 >
Char S [ 5 ], T [ 5 ], Sx, Sy, TX, Ty;
Int A [ 10 ] [ 10 ], DIS [ 10 ] [ 10 ], QX [ 100 ], QY [ 100 ];
Int DX [] = { - 1 , - 2 , - 2 , - 1 , 1 , 2 , 2 , 1 }, Dy [] = { - 2 , - 1 , 1 , 2 , 2 , 1 , - 1 , - 2 };
Int Main ()
{
Int I, X, Y, newx, newy, front, rear, min;
While (Scanf ( " % S % s " , S, T) = 2 )
{
Memset (, - 1 , Sizeof ());
SX = S [ 0 ] - ' A ' ;
Sy = S [ 1 ] - ' 1 ' ;
TX = T [ 0 ] - ' A ' ;
Ty = T [ 1 ] - ' 1 ' ;
Memset (DIS, - 1 , Sizeof (DIS ));
Dis [SX] [sy] = 0 ;
Front = Rear = 0 ;
QX [rear] = SX;
QY [rear] = Sy;
Rear ++ ;
While (Front < Rear)
{
X = QX [Front];
Y = QY [Front];
If (X = TX && Y = Ty)
Break ;
Front ++ ;
For (I = 0 ; I < 8 ; I ++ )
{
Newx = X + DX [I];
Newy = Y + Dy [I];
If (DIS [newx] [newy] < 0 && Newx > = 0 && Newx < 8 && Newy > = 0 && Newy < 8 )
{
Dis [newx] [newy] = Dis [x] [Y] + 1 ;
QX [rear] = Newx;
QY [rear] = Newy;
Rear ++ ;
}
}
}
Min = Dis [TX] [ty];
Printf ( " To get from % s to % s takes % d knight moves. \ n " , S, T, min );
}
Return 0 ;
}