#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使用
张小明
前端开发工程师
AI大洪水来袭!90%的人还在卷算法,聪明的已经盯上“铁饭碗”——协调人
AI大洪水来袭!90%的人还在卷算法,聪明的已经盯上“铁饭碗”——协调人 目录 AI大洪水来袭!90%的人还在卷算法,聪明的已经盯上“铁饭碗”——协调人 🔴 淘汰预警:纯技术“工具人” 🔵 晋升密码:协调型“问题终结者” 我们应该怎么做 做 LLM 技术,这样转型 “技术 + 协…
Python如何识别周围WiFi:跨平台实现与进阶技巧
在物联网设备管理、网络安全审计或智能家居场景中,识别周围WiFi网络是基础需求。Python凭借其丰富的生态库,能够跨平台实现WiFi扫描、信号强度检测及网络分析。本文将系统梳理主流方法,结合代码示例与性能对比,帮助开发者快速构建…
Java毕设项目:基于springboot的助农扶贫系统(源码+文档,讲解、调试运行,定制等)
博主介绍:✌️码农一枚 ,专注于大学生项目实战开发、讲解和毕业🚢文撰写修改等。全栈领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围:&am…
基于spring mvc和mybatis的网上食品零食商城系统视频vue3
目录 系统架构与技术栈核心功能模块技术实现要点数据库设计示例部署与扩展建议 项目技术支持可定制开发之功能亮点源码获取详细视频演示 :文章底部获取博主联系方式!同行可合作 系统架构与技术栈 Spring MVC MyBatis 作为后端框架,Vue3 作为…
常用的css
js控制样式: //html中:<text class"test_text" :style"{--base_color:currentColor}">测试</text>//data中:currentColor:pink //methods中:this.currentColorred//style中:使用变量取值.test_t…
枚举中间
lc lc2964 枚举中间hash class Solution { public: int divisibleTripletCount(vector<int>& nums, int d) { int n nums.size(); if (n < 3) return 0; int ret 0; unordered_map<int, int> hash; hash[nums[0] % d]; // 枚举中间 for (int i 1; i &l…