日结(3.26

A - Super Ryuma

 AtCoder - abc184_c 

There is an infinite two-dimensional grid, and we have a piece called Super Ryuma at square (r_1, c_1)(r1​,c1​). (Ryu means dragon and Ma means horse.) In one move, the piece can go to one of the squares shown below:

a8141d845894fa57ced44db727684a9d?v=1681335117

More formally, when Super Ryuma is at square (a, b)(a,b), it can go to square (c, d)(c,d) such that at least one of the following holds:

  • a + b = c + da+b=c+d
  • a - b = c - da−b=c−d
  • |a - c| + |b - d| \le 3∣a−c∣+∣b−d∣≤3

Find the minimum number of moves needed for the piece to reach (r_2, c_2)(r2​,c2​) from (r_1, c_1)(r1​,c1​).

Constraints

  • All values in input are integers.
  • 1 \le r_1, c_1, r_2, c_2 \le 10^91≤r1​,c1​,r2​,c2​≤109

Input

Input is given from Standard Input in the following format:

r_1r1​ c_1c1​
r_2r2​ c_2c2​

Output

Print the minimum number of moves needed for Super Ryuma to reach (r_2, c_2)(r2​,c2​) from (r_1, c_1)(r1​,c1​).

Sample 1

InputcopyOutputcopy
1 1
5 6
2

We need two moves - for example, (1, 1) \rightarrow (5, 5) \rightarrow (5, 6)(1,1)→(5,5)→(5,6).

Sample 2

InputcopyOutputcopy
1 1
1 200001
2

We need two moves - for example, (1, 1) \rightarrow (100001, 100001) \rightarrow (1, 200001)(1,1)→(100001,100001)→(1,200001).

Sample 3

InputcopyOutputcopy
2 3
998244353 998244853
3

We need three moves - for example, (2, 3) \rightarrow (3, 3) \rightarrow (-247, 253) \rightarrow (998244353, 998244853)(2,3)→(3,3)→(−247,253)→(998244353,998244853).

Sample 4

InputcopyOutputcopy
1 1
1 1
0

 

 

思路:根据题设,我们能从初始点跳往横纵坐标绝对值和小于3的点或横纵坐标移动坐标绝对值相同的点,所以按规则来说到达任意点最多步数为3步,第一步可到位置如图,第二步可到绝对值和小于六的坐标点以及两点的坐标差的和为偶数的点,大于六且为奇则要走三步

#include<iostream>
#include<cstring>
#include<string.h>
#include<cstdio>
#include <string>
#include<math.h>
using namespace std;
int main()
{
    long long int r1,r2;
   long long  int r3,r4;
    cin>>r1>>r2;
    cin>>r3>>r4;
    //printf("%lld\n",abs(r1-r3)+fabs(r2-r4));
    if(r1==r3&&r2==r4)cout<<"0"<<endl;

    else if((abs(r1-r3)+abs(r2-r4)<=3)||(abs(r1-r3)==abs(r2-r4)))cout<<"1"<<endl;
    else if(abs(r1-r3)+abs(r2-r4)<=6)cout<<"2"<<endl;
    else{
        int a1=abs(r1-r3),a2=abs(r2-r4);
        if(a1<a2){a2-=a1;a1=0;if(a2<=3)cout<<"2"<<endl;
        else {
            if(a2%2==1)cout<<"3"<<endl;
            else cout<<"2"<<endl;
        }



        }
        else {a1-=a2;a2=0;if(a1<=3)cout<<"2"<<endl;
         else {
            if(a1%2==1)cout<<"3"<<endl;
            else cout<<"2"<<endl;
        }


        }
    }


}

抽象:

因为多态的存在,子类都可以覆写父类的方法

如果父类的方法本身不需要实现任何功能,仅仅是为了定义方法,目的是让子类去覆写它,那么,可以把父类的方法声明为抽象方法

abstract,表示一个抽象方法

因为抽象方法无法执行,所以不能在类实例化,不过可以使用抽象类实力

抽象类只能被设计来继承它,让子类覆写其方法,起到一种规范作用

5aa90e12df424f2dba1d5d44f0116ef3.png

 接口:纯抽象方法,无字段出现,则可以使用interface可以声明一个接口

方法默认都是public abstract

当一个具体的class去实现一个interface时,需要使用implements关键字

不同于继承,一个类可以有多个接口只要在class  类 implenments 接口名,接口名,接口名

eb9964ddf0fe438c81a8e416bd3089b4.png

 接口之间也可以继承   向上转型

同时接口也可以定义default方法,在子类中不必覆写此方法,但接口不能有字段故

default方法和抽象类的普通方法是有所不同的。因为interface没有字段,default方法无法访问字段,而抽象类的普通方法可以访问实例字段。

静态字段:

是用static修饰的字段,叫静态字段:static field

实例字段在每个实例中都有自己的一个独立“空间”,但是静态字段只有一个共享“空间”,所有实例都会共享该字段

即无论修改哪一个实力的静态字段,其都会更改

静态方法

有静态字段,就有静态方法。用static修饰的方法称为静态方法。

调用实例方法必须通过一个实例变量,而调用静态方法则不需要实例变量,通过类名就可以调用。

ru: person.run();

同时:

因为interface是一个纯抽象类,所以它不能定义实例字段。但是,interface是可以有静态字段的,并且静态字段必须为final类型:

字段只能是public static final类型,因为只能是改类型,所以可以不写修饰符默认是此类型

 类五大成员:类,属性,方法,作用域,内部类

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值