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.
Petya is thinking about the following problem: what minimum number of moves is needed for each of these pieces to move from field(r1, 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 integers r1, 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
4 3 1 6
2 1 3
5 5 5 6
1 0 1
题目大意:
车只能水平和竖直走,象走斜线,王走八个方向。
解题思路:
车和王不说,主要是象。如果起点和终点连线可以构成一个正方形的对角线,则象需要一步,即abs(x1-x2)==abs(y1-y2);如果起点和终点可以通过两步达到,则它媒介一个格子,再到终点,比如(3,1)到(4,6),可以经过(6,4),媒介的这个点会满足一定的条件,即与起点可以构成正方形的对角线,与终点构成一个正方形的对角线,且这两条对角线是互相垂直的,即斜率乘积为-1,即一个为1,一个为-1(因为棋盘的对角线斜率不是1就是-1).
k1=1, y=x+b1, (y1=x1+b1,b1=y1-x1);
k2=-1, y=-x+b2, (y2=-x2+b2,b2=x2+y2);
交点坐标y为y=(b1+b2)/2 ;若存在这样的格子,则y就为整数,表示能找到,此时步数为二,否则为0;
所以如果 (b1+b2)/%2!=0 --> (x2+y2+y1-x1)%2!=0,则 步数为0,其他情况为2.
- #include<iostream>
- #include<cstdio>
- #include<algorithm>
- #include<cmath>
- using namespace std;
- int main(){
- int x1,x2,y1,y2;
- while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2)!=EOF){
- int a=1,b=2,c=max(abs(x1-x2),abs(y1-y2));//Max头文件algorithm,abs头文件cmath.
- if(x1!=x2&&y1!=y2)a=2;
- if(abs(x1-x2)==abs(y1-y2))
- b=1;
- else if((x2+y2+(y1-x1))%2!=0)
- b=0;
- printf("%d %d %d\n",a,b,c);
- }
- return 0;
- }