• Lintcode 3656 · Design Snake Game (贪吃蛇,设计题)


    3656 · Design Snake Game
    Medium

    Description
    In this question, you need to design a snake game class SnakeGame.

    Snake Game: There exists a snake in a matrix. Food will appear continuously in this matrix, and we need to control the snake to move continuously (through the four directions of up, down, left, and right) to eat the food. after eating food. The length of the snake will increase by one bit. At the same time, the game score will be increased by 1. Afterwards, the next food will be refreshed (the position occupied by the snake will not be refreshed). If the snake hits the wall or eats its own body while eating the food, the game is over.

    The function’s constructor receives the following parameters:

    width: the number of columns in the snake matrix interface
    height: the number of rows in the snake matrix interface
    foods: The coordinates of the food, represented by a two-dimensional array, where the first element of each array is the row coordinate, and the second element is the column coordinate
    In addition, you need to complete the move() function, which will receive a string indicating the direction. Indicates that the snake needs to move one space in this direction. You need to return the Snake score after the move (initial score is 0). Returns -1 if the game ends after the snake moves.

    Only $39.9 for the “Twitter Comment System Project Practice” within a limited time of 7 days!

    WeChat Notes Twitter for more information(WeChat ID jiuzhang104)

    Example
    Example 1

    Input:

    3
    2
    [[1, 2], [0, 0]]
    [“r”, “d”, “r”, “u”, “l”, “l”, “l”]
    Output:

    [0, 0, 1, 1, 1, 1, 2, -1]
    Explanation:

    The initial state is as follows:

    s | |
    | | f
    Among them, s represents the snake, and f represents the current food.

    After action “r”:

    | s |
    | | f

    After eating the first food:

    f | |
    | s |s

    After eating the second food:

    s | s | s
    | |
    Example 2

    Input:

    3
    2
    [[0, 1], [1, 1], [1, 0], [0, 2]]
    [“r”, “d”, “l”, “u”, “r”, “r”, “d”, “l”, “u”]
    Output:

    [1, 2, 3, 3, 3, 4, 4, 4, -1]

    解法1:蛇的身体用deque。另外再用set判重(如果吃到自己的身体的话)。
    食物可以用queue,因为是按先后顺序一个一个出现。
    为什么蛇的身体用deque呢,因为吃到食物要pop_tail,而在游走的过程中要pop_front。

    class SnakeGame {
    public:
        SnakeGame(int width, int height, vector<vector<int>>& foods) : width(width), height(height) {
            for (auto food : foods) {
                foodQueue.push({food[0], food[1]});
            }
            currPos = {0, 0}; //initial position (0,0)
            score = 0;
        }
    
        /**
         * @param direction: the direction of the move
         * @return: the score after the move
         */
        int move(string &direction) {
            int n = direction.size();
            switch (direction[0]) {
                case 'r':
                    currPos.second++;
                    break;
                case 'l':
                    currPos.second--;
                    break;
                case 'd':
                    currPos.first++;
                    break;
                case 'u':
                    currPos.first--;
                    break;
            }
            if (currPos.first < 0 || currPos.first >= height) return -1;
            if (currPos.second < 0 || currPos.second >= width) return -1;
            if (bodySet.find(currPos) != bodySet.end()) return -1;
            bodySet.insert(currPos);
            bodyQueue.push_back(currPos);
            if (!foodQueue.empty() && currPos == foodQueue.front()) {
                foodQueue.pop();
                score++;
            } else {
                bodySet.erase(bodyQueue.front());
                bodyQueue.pop_front();
            }
           
            return score;
        }
    private:
        queue<pair<int, int>> foodQueue;
        int width, height;
        set<pair<int, int>> bodySet; // snake body set
        deque<pair<int, int>> bodyQueue; // snake body queue
        pair<int, int> currPos;
        int score;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
  • 相关阅读:
    基于JavaSwing开发围棋游戏+论文+PPT+任务书 课程设计 大作业 毕业设计
    jar增量打包
    #ArrayList 和 LinkedList 的区别(超容易理解)
    Elasticsearch深入理解(七) —— Index Setting一览
    Vue3 企业级优雅实战 - 组件库框架 - 12 发布开源组件库
    k8s之service五种负载均衡byte的区别
    java学习第三周总结。。。
    mysql--两个查询结果合并到一起,两表无关联关系。
    通过nginx访问另一台服务器上的图片文件
    arduino - UNO-R3,mega2560-R3,NUCLEO-H723ZG的arduino引脚定义区别
  • 原文地址:https://blog.csdn.net/roufoo/article/details/133579347