Poj 1915 knight moves

Source: Internet
Author: User

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 ;
}

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.