当前位置: 首页 > news >正文

贪吃蛇 set和deque使用

#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; } };
http://www.jsqmd.com/news/294412/

相关文章:

  • 亲测好用8个一键生成论文工具,研究生论文写作必备!
  • 电力企业数字化管理升级,如何实现项目、人员、财务一体化管控?
  • 轰炸敌人,最多可以摧毁的敌人城堡数目
  • 汉诺塔问题及其扩展
  • 揭秘天猫超市购物卡回收其中的猫腻
  • Spring Cloud Alibaba 2025.0.0 整合 ELK 实现日志 - 详解
  • 三分之一2-5天和三分之二6-13天资金利用率对比学习
  • Android关机
  • 221_尚硅谷_实现接口和继承比较(2)
  • 2026年苏州智能硬件设计公司推荐:飓风工业设计,企业产品设计/专业工业设计/产品外观设计/电子产品设计/工业设计/机械产品设计公司精选
  • 2026年国内知名的投影机品牌排名,激光投影仪/20000流明投影机出租/画展投影机出租/雾幕投影机,投影机公司排行
  • JAVA自学之路1.1:JAVA入门纠错
  • 2026软考高级系统架构师备考资料-录播+直播
  • 微信立减金回收攻略,方法、流程与折扣全解析
  • 安达发|精准排产,守护生命:医疗器械行业车间排产的数字化革命
  • 2026年市场评价好的纸盒品牌推荐排行,纸盒/彩印包装/农产品纸箱/工业纸盒/纸箱/工业纸箱,纸盒批发厂家推荐排行
  • Deepseek问答:开发人员如何选书
  • php开源短视频源码,JSON对象转化API
  • 短视频平台php源码,字符缓冲流的特有功能
  • 短视频app搭建,如何实现毛玻璃效果?
  • 小视频平台源码,ElementUI 本地分页
  • C 语言基础:输入输出、运算符与流程控制全梳理
  • 鸿蒙APP开发从入门到精通:ArkUI组件库详解与常用组件实战
  • Typescript——泛型
  • Win11 轻松设置更新暂停至 2042年告别过度弹窗 卸载系统冗余软件
  • 实战复盘:如何用 HTML+JS+AI 打造一款“影迹”智能影视管理系统
  • 从对象结构到锁机制:Java 对象锁与类锁深度解析
  • 什么是 Java 中的原子性、可见性和有序性?
  • 2000亿美元!2026年全球游戏行业的新格局与新变量
  • 使用 wxPython 构建文件编辑器与预览器:从零到一的完整实现