CodeForces 370A Rook, Bishop and King (minimum steps from car, elephant, King to destination)

Source: Internet
Author: User

CodeForces 370A Rook, Bishop and King (minimum steps from car, elephant, King to destination)
Rook, Bishop and KingTime Limit:1000 MSMemory Limit:262144KB64bit IO Format:% I64d & % I64u

Description

Little Petya is learning to play chess. he has already learned how to move a king, a rook and a bishop. let us remind you the rules of moving chess pieces. A chessboard is 64 square fields organized into an 8? ×? 8 table. A field is represented by a pair of integers (R,?C)-The number of the row and the number of the column (in a classical game the columns are traditionally indexed by letters ). each chess piece takes up exactly one field. to make a move is to move a chess piece, the pieces move by the following rules:

A rook moves any number of fields horizontally or vertically. A bishop moves any number of fields diagonally. A king moves one field in any direction-horizontally, vertically or diagonally. The pieces move like that

Petya is thinking about the following problem: what minimum number of moves is needed fZ limit? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> lower" lower-index "> 1 ,?C1) to field (R2 ,?C2 )? At that, we assume that there are no more pieces besides this one on the board. Help him solve this problem.

Input

The input contains four integersR1 ,?C1 ,?R2 ,?C2 (1? ≤?R1 ,?C1 ,?R2 ,?C2? ≤? 8)-the coordinates of the starting and the final field. The starting field doesn' t coincide with the final one.

You can assume that the chessboard rows are numbered from top to bottom 1 through 8, and the columns are numbered from left to right 1 through 8.

Output

Print three space-separated integers: the minimum number of moves the rook, the bishop and the king (in this order) is needed to move from field (R1 ,?C1) to field (R2 ,?C2). If a piece cannot make such a move, print a 0 instead of the corresponding number.

Sample Input

Input
4 3 1 6
Output
2 1 3
Input
5 5 5 6
Output
1 0 1

Question:

The car can only go horizontally and vertically, like a diagonal line, and the king goes in eight directions.

Solution:

The car and Wang did not say, mainly like. If the start and end line can form a diagonal line of a square, it is like a step, that is, abs (x1-x2) = abs (y1-y2); If the start and end can be achieved through two steps, then it mediates a grid and then to the destination. For example, (3, 1) to (4, 6), it can pass through (6, 4). This point of media will meet certain conditions, that is, it can form the diagonal line of a square with the starting point, and form the diagonal line of a square with the ending point, and the two diagonal lines are perpendicular to each other, that is, the product of the slope is-1, that is, one is 1, one is-1 (because the diagonal slope of the board is not 1 or-1 ).

K1 = 1, y = x + b1, (y1 = x1 + b1, b1 = y1-x1 );

K2 =-1, y =-x + b2, (y2 =-x2 + b2, b2 = x2 + y2 );

The intersection coordinate y is y = (b1 + b2)/2. If such a grid exists, y is an integer, indicating that it can be found. At this time, the number of steps is two. Otherwise, it is 0;

So if (b1 + b2)/% 2! = 0 --> (x2 + y2 + y1-x1) % 2! = 0, the number of steps is 0, and in other cases, 2.


[Cpp]View plaincopy
  1. # Include
  2. # Include
  3. # Include
  4. # Include
  5. Using namespace std;
  6. Int main (){
  7. Int x1, x2, y1, y2;
  8. While (scanf ("% d", & x1, & y1, & x2, & y2 )! = EOF ){
  9. Int a = 1, B = 2, c = max (abs (x1-x2), abs (y1-y2); // Max header file algorithm, abs header file cmath.
  10. If (x1! = X2 & y1! = Y2) a = 2;
  11. If (abs (x1-x2) = abs (y1-y2 ))
  12. B = 1;
  13. Else if (x2 + y2 + (y1-x1) % 2! = 0)
  14. B = 0;
  15. Printf ("% d \ n", a, B, c );
  16. }
  17. Return 0;
  18. }

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.