#include <vector> #include <string> #include <deque> #include <set> using namespace std; // 您提供的 Node 结构体 typedef struct Node{ int _x; int _y; Node(int x, int y) { _x = x; _y = y; } // 重载 < 运算符,方便放入 set 中进行去重/查找 bool operator<(const Node& other) const { if (_x != other._x) return _x < other._x; return _y < other._y; } // 重载 == 运算符 bool operator==(const Node& other) const { return _x == other._x && _y == other._y; } } Node; class SnakeGame { private: int width; int height; int score; vector<vector<int>> food; // 存储食物列表 int foodIndex; // 当前该吃第几个食物 deque<Node> snake; // 蛇身:front是头,back是尾 set<Node> snakeBodySet; // 快速查找碰撞(辅助结构) public: /** Initialize your data structure here. @param width - screen width @param height - screen height @param food - A list of food positions E.g food = [[1,2], [0,1]] */ SnakeGame(int width, int height, vector<vector<int>>& food) { this->width = width; this->height = height; this->food = food; this->foodIndex = 0; this->score = 0; // 初始化蛇,起初在 (0,0) Node startNode(0, 0); snake.push_front(startNode); snakeBodySet.insert(startNode); } /** Moves the snake. @param direction - 'U' = Up, 'L' = Left, 'R' = Right, 'D' = Down @return The game's score after the move. Return -1 if game over. Game over when snake crosses the screen boundary or bites its body. */ int move(string direction) { // 1. 获取当前蛇头位置 Node head = snake.front(); int next_y = head._y; // 题目中通常 y 代表行 (row) int next_x = head._x; // 题目中通常 x 代表列 (col) // 2. 根据方向计算新蛇头的位置 if (direction == "U") next_y--; else if (direction == "D") next_y++; else if (direction == "L") next_x--; else if (direction == "R") next_x++; // 3. 边界碰撞检测 (撞墙) if (next_y < 0 || next_y >= height || next_x < 0 || next_x >= width) { return -1; } Node newHead(next_x, next_y); // 4. 处理蛇尾逻辑(关键点!) // 先把尾巴拿掉,因为如果只是单纯移动,尾巴的位置是安全的(蛇头可以追着尾巴走) // 如果吃到了食物,再把尾巴加回来 Node tail = snake.back(); snake.pop_back(); snakeBodySet.erase(tail); // 5. 身体碰撞检测 (撞自己) // 注意:这里必须在移除尾巴之后检查,因为新头的位置可以是旧尾巴的位置 if (snakeBodySet.count(newHead)) { return -1; // 撞到自己了 } // 6. 检查是否吃到食物 if (foodIndex < food.size() && next_y == food[foodIndex][0] && next_x == food[foodIndex][1]) { // 吃到食物了: score++; foodIndex++; // 既然吃到了食物,蛇变长,刚才移除的尾巴得加回来! snake.push_back(tail); snakeBodySet.insert(tail); } // 7. 更新状态:加入新头 snake.push_front(newHead); snakeBodySet.insert(newHead); return score; } };贪吃蛇 set和deque使用
张小明
前端开发工程师
logging模块,scrapy全站爬取
1.longging模块 logging是python内置的日志处理模块 在scrapy中,可以配置LOG_LEVEL来设置输出的日志等级,也可以在scrapy/settin/default_setting.py 路径下查看日志相关默认参数(需要修改的参数可以直接在settings.py文件中修改) #添加日志 LOG_LEVEL="WAREING"…
权利的本质是插队?县城婆罗门的毛细血管,我们该走还是留?
权利的本质是插队?县城婆罗门的毛细血管,我们该走还是留? 目录 权利的本质是插队?县城婆罗门的毛细血管,我们该走还是留?不是宗教意义上的阶层,而是基层独有的生态于是很多人心里都冒出来一个天…
电力企业数字化管理升级,如何实现项目、人员、财务一体化管控?
某民企电力企业,是集电力设计、电力工程、电力设备、电力运维于一体的专业电力服务商,服务网点覆盖全国。企业人员规模500,项目覆盖众多乡村及城市区域,面临着人员、项目等多维度的管理挑战。 一、企业管理痛点 项目信息搜集低效…
TPDO vs RPDO 对比总结
TPDO vs RPDO 对比总结 核心本质对比 维度 TPDO RPDO 名字全称 Tx Process Data Object Rx Process Data Object 通信方向 Slave → Master (从站上传主站) Master → Slave (主站下发从站) 通信内容 状态反馈: 位置、速度、力矩等 控制指令: 目标位置、模式切换 在 RTLink 中…
短视频app搭建,如何实现毛玻璃效果?
短视频app搭建,如何实现毛玻璃效果? <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta http-equiv"X-UA-Compatible" content"IEedge"><meta name"vi…
Deepseek问答:开发人员如何选书
如果觉得好,请点赞、收藏、转发和关注,感谢您!仅供参考问:按照开发人员的用途、职位、技能掌握程度、预算、可支配时间五个方面提出选择编程书籍的技巧ds答:为不同背景和需求的开发人员选择编程书籍是一项需要精细匹配…