CodeForces 370A Rook, Bishop and King(车,象,王到目的地的最小步数)

Rook, Bishop and King
Time Limit:1000MS     Memory Limit:262144KB     64bit 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 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

Input
4 3 1 6
Output
2 1 3
Input
5 5 5 6
Output
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.


[cpp]  view plain copy
  1. #include<iostream>  
  2. #include<cstdio>  
  3. #include<algorithm>  
  4. #include<cmath>  
  5. using namespace std;  
  6.   
  7. int main(){  
  8.     int x1,x2,y1,y2;  
  9.     while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2)!=EOF){  
  10.         int a=1,b=2,c=max(abs(x1-x2),abs(y1-y2));//Max头文件algorithm,abs头文件cmath.  
  11.         if(x1!=x2&&y1!=y2)a=2;  
  12.         if(abs(x1-x2)==abs(y1-y2))  
  13.             b=1;  
  14.         else if((x2+y2+(y1-x1))%2!=0)  
  15.             b=0;  
  16.         printf("%d %d %d\n",a,b,c);  
  17.     }  
  18.     return 0;  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值