hzwer jumping chess

[Title Description:

Hzwer jumping game is carried out in a number line. Pieces can only be placed on the whole point. Each point can not put more than one piece.

One day, the god of gold and cjy do with a simple jumping chess game: There are three pieces on the board, respectively, in a, b, c three positions. They minimal beating them into the position x, y, z. (Pawn is no difference)

The rules are simple beat, a pawn in any election, beating on a piece axis. After beating two pieces the same distance. Allowing only skip a piece.

o a o x o b o o 

For example: a transferred to the central axis x b.

Write a program, first determine if you can complete the task. If you can, it requires a minimum output of beats.

[Input Description:

The first line contains three integers, indicating the current position of the pieces abc. (Different from each other)

The second line contains three integer indicating the target position xyz. (Different from each other)

[Output] Description:

If no solution, output line NO.

If arrival, the first line of the output YES, the minimum number of steps of the second output line.

[Sample input]:

1 2 3
0 3 5

[] Sample output:

YES
2

[Sample] Description:

[Time limit, and the range of data Description:

Time: 1s space: 256M

20% of the absolute value of the input integer not more than 10

40% of the absolute value of the input integer not more than 10,000

100% of the absolute value is not more than 10 ^ 9

 

Do serious protest tree sectional class of topics! ! !

 

#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;
}

 

Guess you like

Origin www.cnblogs.com/hrj1/p/11231015.html