D - Checkerboard

Byung is playing a game on her infinite checkerboard. The goal of the game is to move from a starting position ( r s, c s) to a target position ( r t, c t) 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), r s, c s, r t and c t ( - 109 ≤ r s, c s, r t, c t ≤ 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.

Examples
Input
5 1 1 2 3

Output
2

Input
500 -100 -100 100 100

Output
51

给定一个限度n,和两个点的坐标;
计算出在限度n中,有多少步数可以由起点到达终点。

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

int main()
{
    long long int l1,l2,r1,r2;
    long long int n,s,s1,s2;
    scanf("%lld",&n);
    scanf("%lld %lld %lld %lld",&l1,&l2,&r1,&r2);
    s1=r1-l1;
    s2=r2-l2;
    if(s1<0){
        s1=-s1;
    }
    if(s2<0){
        s2=-s2;
    }
    s=s1+s2;
    if(s<0)
        s=-s;
    if(s>0)
    {
        if(n<s)
        {
            printf("0\n");
        }
        else
        {
            n=n-s+2;
            n=n/2;
            printf("%lld\n",n);
        }
    }
    else{
        n=n/2;
        printf("%lld\n",n);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值