Stay Hungry,Stay Foolish!

C - Loong Tracking

C - Loong Tracking

题目传送门

https://atcoder.jp/contests/abc335/tasks/abc335_c

 

思路

贪吃蛇,头部不断更新, 原来的头部会变成颈部,颈部变成胸膛,..., 原来的尾部删除。

采用双端队列,

头部 根据 头部做更新

尾部删除。

Code

https://atcoder.jp/contests/abc335/submissions/49143475

struct NODE{
    int x;
    int y;
};

deque<struct NODE> dq;

int n,q;

int main()
{
    cin >> n;
    cin >> q;

    for(int i=1; i<=n; i++){
        struct NODE nd;
        nd.x = i;
        nd.y = 0;
        
        dq.push_back(nd);
    }

    for(int i=0; i<q; i++){
        int action;
        cin >> action;
        
        if (action == 1){
            char c;
            cin >> c;

            struct NODE f;
            f = dq.front();

            if (c == 'U'){
                f.y++;
            } else if (c == 'D') {
                f.y--;
            } else if (c == 'L') {
                f.x--;
            } else if (c == 'R') {
                f.x++;
            } else {
                // no other branch should happen
            }
            
            dq.push_front(f);
            dq.pop_back();
        } else if (action == 2){
            int p;
            cin >> p;
            
            cout << dq[p-1].x << " " << dq[p-1].y << endl;
        }
    }

    return 0;
}

 

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