Stay Hungry,Stay Foolish!

C - Tile Distance 2

C - Tile Distance 2

https://atcoder.jp/contests/abc359/tasks/abc359_c

 

思路

在x方向上,让s<t

然后 如果s在tile的左边,移动到右边,  如果t在tile的右边,移动到左边,

计算x 和 y方便的必走的steps,

y方向上容易计算(跨的格子就是), x方向有些复杂, s在x方向上,不用花费(配合y方向上走步),可延伸到最大位置为 sx+ysteps,

如果tx在此最大位置之内,则x方向不用花费, 否则必须花费。

 

t相对s三种位置。

 

 

Code

https://atcoder.jp/contests/abc359/submissions/54883741

long long sx, sy, tx, ty;

int main()
{
    cin >> sx >> sy >> tx >> ty;
    
    /*
    keep source before target in x direction
    */
    if (sx > tx){
        swap(sx, tx);
        swap(sy, ty);
    }

    /*
    if (sx,sy) in the left side of one brick
    i.e. sx+sy is even
    -------
    |s |  |
    -------
    then move s to its right tile, like below
    -------
    |  |s |
    -------
    */
    if ((sx+sy)&1 == 0){
        sx++;
    }
    
    /*
    if (tx,ty) in the right side of one brick
    i.e. tx+ty is odd
    -------
    |  |s |
    -------
    then move s to its left tile, like below
    -------
    |s |  |
    -------
    */
    if ((tx+ty)&1 == 1){
        tx--;
    }

    // xdiff must be not less than zero.
    // ydiff is of any value
    long long xdiff = tx - sx;
    long long ydiff = ty - sy;

    /*
    caculate the step of x-neccessary steps
    caculate the step of y-neccessary steps
    */
    long long xsteps = 0;
    long long ysteps = abs(ydiff);

    /*
    if x position of target is greater than sx extent scope,
    than x-neccessary steps are needed.
    */
    long long sx_extent = sx + ysteps;
    if (tx > sx_extent){
        xsteps = (tx - sx_extent + 1)/2;
    }

    long long total = xsteps + ysteps;
    cout << total << endl;

    return 0;
}

 

posted @ 2024-06-23 22:50  lightsong  阅读(12)  评论(0编辑  收藏  举报
Life Is Short, We Need Ship To Travel