设为首页 加入收藏

TOP

CodeForces 370A Rook, Bishop and King(车,象,王到目的地的最小步数)
2015-07-20 17:55:45 来源: 作者: 【 】 浏览:2
Tags: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 fZ??http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vciBlYWNoIG9mIHRoZXNlIHBpZWNlcyB0byBtb3ZlIGZyb20gZmllbGQoPGVtPnI8L2VtPjxzdWIgY2xhc3M9"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 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 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%d%d%d",&x1,&y1,&x2,&y2)!=EOF){
  9. int a=1,b=2,c=max(abs(x1-x2),abs(y1-y2));//Max头文件algorithm,abs头文件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 %d %d\n",a,b,c);
  16. }
  17. return 0;
  18. }
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇学习日记之职责链模式和Effective.. 下一篇hdu 4300 Clairewd’s message (K..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: