hzwer's checkers

Posted by sunwukung on Fri, 18 Oct 2019 20:45:29 +0200

[Title Description]:

Hzwer's checkers are played on a number axis. Chessmen can only be placed on the whole point. No more than one chess piece can be placed at each point.

One day, the golden God and cjy used checkers to make a simple game: there are three pieces on the board, respectively in a, b and c positions. They're going to move their positions to x, y, z with the least bounce. (there is no difference between pieces)

The rule of jumping is very simple. Choose a piece at will and jump to a middle axis piece. After jumping, the distance between the two pieces remains the same. Only one piece can be skipped at a time.

o a o x o b o o 

For example: a takes x as the central axis and adjusts to b.

Write a program, first determine whether the task can be completed. If possible, output the minimum required number of beats.

[enter description]:

The first line contains three integers, representing the position a b c of the current chess piece. (different from each other)

The second line contains three integers representing the target position x y z. (different from each other)

[output description]:

If there is NO solution, output a line of NO.

If it can be reached, the first line outputs YES, and the second line outputs the minimum number of steps.

[sample input]:

1 2 3
0 3 5

[sample output]:

YES
2

[sample description]:

[time limit, data scope and description]:

Time: 1s space: 256M

The absolute value of 20% input integers shall not exceed 10

The absolute value of 40% input integers shall not exceed 10000

100% absolute value not more than 10 ^ 9

 

Spicy chicken section

#include <cstdio>
#include <algorithm>
int min(int x,int y){return x<y?x:y;}
int r[3],ori[3],goa[3];
int get(int a,int b,int c)
{
    int d1=b-a,d2=c-b,cnt=0;
    if(d1>d2)
    {
        cnt=d1/d2;
        int d=d1%d2;
        if(!d)
        {
            d+=d2;
            cnt--;
        }
        cnt+=get(a,a+d,a+d+d2);
    }
    else if(d1<d2)
    {
        cnt=d2/d1;
        int d=d2%d1;
        if(!d)
        {
            d+=d1;
            cnt--;
        }
        cnt+=get(c-d-d1,c-d,c);
    }
    else
        r[0]=a,r[1]=b,r[2]=c;
    return cnt;
}
void up(int a,int b,int c,int step)
{
    if(!step)
    {
        r[0]=a,r[1]=b,r[2]=c;
        return;
    }
    int d1=b-a,d2=c-b,cnt=0;
    if(d1>d2)
    {
        cnt=d1/d2;
        int d=d1%d2;
        if(!d)
        {
            d+=d2;
            cnt--;
        }
        if(step>=cnt)
            up(a,a+d,a+d+d2,step-cnt);
        else
        {
            int k=cnt-step;
            up(a,a+d+k*d2,a+d+(k+1)*d2,0);
        }
    }
    else if(d1<d2)
    {
        cnt=d2/d1;
        int d=d2%d1;
        if(!d)
        {
            d+=d1;
            cnt--;
        }
        if(step>=cnt)
            up(c-d-d1,c-d,c,step-cnt);
        else
        {
            int k=cnt-step;
            up(c-d-(k+1)*d1,c-d-k*d1,c,0);
        }
    }
    else
        r[0]=a,r[1]=b,r[2]=c;
}
bool check(int step)
{
    int to[3];
    up(goa[0],goa[1],goa[2],step);
    to[0]=r[0];to[1]=r[1];to[2]=r[2];
    up(ori[0],ori[1],ori[2],step);
    if(to[0]!=r[0]||to[1]!=r[1]||to[2]!=r[2])
        return false;
    return true;
}
int main()
{
    int to[3],ans=0;
    scanf("%d%d%d%d%d%d",ori,ori+1,ori+2,goa,goa+1,goa+2);
    std::sort(ori,ori+3);std::sort(goa,goa+3);
    int step1=get(ori[0],ori[1],ori[2]);
    to[0]=r[0];to[1]=r[1];to[2]=r[2];
    int step2=get(goa[0],goa[1],goa[2]);
    if(to[0]!=r[0]||to[1]!=r[1]||to[2]!=r[2])
    {
        printf("NO\n");
        return 0;
    }
    if(step1<step2)
    {
        ans+=step2-step1;
        up(goa[0],goa[1],goa[2],step2-step1);
        goa[0]=r[0];goa[1]=r[1];goa[2]=r[2];
    }
    else if(step1>step2)
    {
        ans+=step1-step2;
        up(ori[0],ori[1],ori[2],step1-step2);
        ori[0]=r[0];ori[1]=r[1];ori[2]=r[2];
    }
    int l=0,rr=min(step1,step2);
    while(l<rr)
    {
        int mid=l+rr>>1;
        if(check(mid))
            rr=mid;
        else
            l=mid+1;
    }
    printf("YES\n%d\n",(l<<1)+ans);
    return 0;
}

Topics: PHP