Codeforces #217 (Div. 2) A Rook, Bishop and King

本文探讨了在国际象棋中,如何计算车、象、王从一个指定位置移动到另一个指定位置所需的最少步数。文章通过具体示例介绍了不同棋子的移动规则,并提供了一段实现这些规则的C语言代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 test(s)
input
4 3 1 6
output
2 1 3
input
5 5 5 6
output
1 0 1


第二次做codeforces,坑得要死有木有大哭

尼玛就做出一题,最后还错了!!

题目的意思是对三种棋子有三种移动方法:

1、同行或同列随意移动步数

2、对角线上随意移动步数

3、任意方向移动一步

给出(r1,c1)    (r2,c2)

保证两坐标不同

输出三种移动方式下的最小步数

主要还是心态没有调整好,一心想着早点交导致错误和底下各种问题。

分析第一种情况:若行和列有一个相等的,则移动一次;两者均不同则移动两次。

分析第二种情况:可知一共有0、1、2三种可能,无法移动到则为0,在其主对角线上则为1,移动到对角线上某点后再以该位置为起始位置移动对角线为2;

结果为1对应的为abs(r1-r2)==abs(c1-c2);

结果为2对应的为abs(r1-r2)-abs(c1-c2)为2的倍数且不为0;

不符合两种情况的则对应0

分析第三种情况:若r1==r2,则移动abs(c1-c2);若c1==c2,则移动abs(r1-r2);若均不相等,则可分析得只需移动abs(c1-c2)和abs(r1-r2)中的较大者步即可!


在写博客的过程中才意识到最大的错误是没有看这个图!!!!!!!!!!发火

怒贴一记代码:

#include <stdio.h>
#include <stdlib.h>

int max(int a,int b)
{ return a>b?a:b;}

int main(void)
{
    int r1,c1,r2,c2;
    int a,b;
    while(scanf("%d%d%d%d",&r1,&c1,&r2,&c2)!=EOF)
    {
     int flag1=0,flag2=0,flag3=0;
     a=abs(r1-r2);
     b=abs(c1-c2);
     if(r1!=r2)
      flag1++;
     if(c1!=c2)
      flag1++;   
     if(abs(a-b)%2==0)
     {
      if(a==b)
       flag2=1;
      else
       flag2=2;
     }
     if(r1==r2)
      flag3=b;
     else if(c1==c2)
      flag3=a;
     else flag3=max(a,b);
     printf("%d %d %d\n",flag1,flag2,flag3);
    }
    return 0;
} 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值