Checkerboard

Byung is playing a game on her infinite checkerboard. The goal of the game is to move from a starting position (rs, cs) to a target position (rt, ct) using exactly x moves, where x is determined by a roll of a N-sided dice.

A move consists in going from a square on the checkerboard to one of its four neighbors - up, down, left or right.

Her dice is unbiased, meaning that every number from 1 to N has the same probability of being rolled.

She's interested in knowing what's the number of different rolls that allow her to move to the target position in the last move. She can move to the same square more than once if she needs to. Can you help her?

Input

The input is composed by a single line containing 5 integers N (1 ≤ N ≤ 109), rs, cs, rt and ct ( - 109 ≤ rs, cs, rt, ct ≤ 109) indicating respectively the number of sides of the dice, the coordinates of the starting position and the coordinates of the target position.

Output

An integer number indicating the number of outcomes that she'll get a good roll.

 

最小步数移动到目标后,其他正好到达目标点的步数为当前最小步数加2的倍数。

#include <iostream>
using namespace std;
typedef long long ll;
int main()
{
    ios::sync_with_stdio(false);
    int a, b, c, d, n;
    cin>>n>>a>>b>>c>>d;
    ll sum = (ll)(a-c > 0 ? a-c : c-a) + (b-d > 0 ? b - d: d - b);//计算时必须转换成long long 类型, 加合会超出int
    if(sum > n)
    {
        cout<<0<<endl;
    }
    else if(sum==0)
    {
        cout<<(n>>1)<<endl;
    }
    else
    {
        cout<<((n-sum)>>1)+1<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值