Poj 2243 knight moves

Source: Internet
Author: User

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

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.