Stay Hungry,Stay Foolish!

abc361

A - Insert

https://atcoder.jp/contests/abc361/tasks/abc361_a

https://atcoder.jp/contests/abc361/submissions/55260626

int n, k, x;
vector<int> a;

int main()
{
    cin >> n >> k >> x;

    a.resize(n+1);

    for(int i=0; i<n+1; i++){
        if (i == k){
            a[i] = x;
        } else {
            cin >> a[i];
        }
    }

    for(int i=0; i<n+1; i++){
        cout << a[i] << " ";
    }

    return 0;
}

 

B - Intersection of Cuboids

https://atcoder.jp/contests/abc361/tasks/abc361_b

https://atcoder.jp/contests/abc361/submissions/55334640

 

https://gamedev.stackexchange.com/questions/130408/what-is-the-fastest-algorithm-to-check-if-two-cubes-intersect-where-the-cubes-a

Separating axis test. (Also called separating axis theorem, separating axis theorem test, yada yada.)

If you can find an axis (ANY axis), not just regular XYZ basis vectors) where the projections of your two cubes don't overlap, then they don't intersect.

There are a ton of tutorials and implementations available via Google, but this one has pretty graphs:

https://gamedevelopment.tutsplus.com/tutorials/collision-detection-using-the-separating-axis-theorem--gamedev-169

This is what we teach our game dev grad students and they all get jobs. :)

Also, checking intersections of vertices and faces or edges doesn't cover the case where cubes are nested, so it's not a real solution. SAT looks complicated in 3D, but it's just a lot of repetition. Cut, paste, trace and try some edge cases and you'll get it.

A dirt-simple method that's good enough for broad-phase collision detection is to treat the cubes as spheres. (If they're really regular cubes, the error is marginal, though proportional to the scale of the cubes relative to each other.)

Get the distance between the center of the cubes, and see if it is greater than the sum of the half diagonals of the two cubes. (Basically, you're checking bounding spheres instead of worrying about hyperplanes.)

 

int a, b, c, d, e, f;
int g, h, i, j, k, l;

bool check_if_intersected(array<int, 2>& one_segment, array<int, 2>& two_segment){
    int one_start = one_segment[0];
    int one_stop = one_segment[1];
    
    int two_start = two_segment[0];
    int two_stop = two_segment[1];

    /*
    intersection:
    one        ---------------
    two         -------------
    */
    if (two_start >= one_start){
        if (one_stop > two_start){
            return true;
        }
    }

    /*
    intersection:
    one            ---------------
    two     -------------
    */
    if (one_start >= two_start){
        if (two_stop > one_start){
            return true;
        }
    }
    
    return false;
}

int main()
{
    cin >> a >> b >> c >> d >> e >> f;
    cin >> g >> h >> i >> j >> k >> l;

    array<array<int,2>, 3> dimensions_of_one{{
        {a, d}, // x
        {b, e}, // y
        {c, f}  // z
    }};

    array<array<int,2>, 3> dimensions_of_two{{
        {g, j}, // x
        {h, k}, // y
        {i, l}  // z
    }};

    for(int i=0; i<3; i++){
        array<int, 2>& one_segment = dimensions_of_one[i];
        array<int, 2>& two_segment = dimensions_of_two[i];
        /*
        check if two segment has intersection.
        */
        if (!check_if_intersected(one_segment, two_segment)){
            cout << "No" << endl;
            return 0;
        }
    }

    cout << "Yes" << endl;

    return 0;
}

 

posted @ 2024-07-07 10:59  lightsong  阅读(11)  评论(0编辑  收藏  举报
Life Is Short, We Need Ship To Travel