news 2026/5/11 16:47:19

贪吃蛇 set和deque使用

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
贪吃蛇 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; } };
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/11 13:09:13

轰炸敌人,最多可以摧毁的敌人城堡数目

我的解法&#xff1a; 对于每一个空位置&#xff0c;进行一次bfs&#xff0c;从上下左右四个方向去寻找&#xff0c;这是岛屿问题的相似处理&#xff0c;但是这个题可以优化&#xff0c;因为横竖方向可以进行动规优化。 我们不需要对每个 0 都重新数一遍它所在的行和列有多少敌…

作者头像 李华
网站建设 2026/5/9 22:40:16

微信小程序 == rsa加解密工具

wxmp-rsa 1、简介 前端rsa加解密工具。 基于jsencrypt修改扩展功能。兼容小程序环境&#xff0c;压缩后60kb左右的大小&#xff0c;节省小程序空间。支持超长文本加解密。支持中文字符的加解密。 仓库地址 https://github.com/jiayc4215/wxmp-rsa2、安装 npm i wxmp-rsa …

作者头像 李华
网站建设 2026/5/10 5:47:04

安达发|精准排产,守护生命:医疗器械行业车间排产的数字化革命

在关乎人类生命健康的医疗器械制造领域&#xff0c;每一台设备、每一个部件的准时交付&#xff0c;都可能直接影响到患者的治疗与康复。这个行业以其严苛的质量法规&#xff08;如FDA、ISO 13485&#xff09;、复杂的生产工艺和极高的可追溯性要求而著称。在这样的背景下&#…

作者头像 李华
网站建设 2026/5/9 6:48:41

Spring MVC核心流程深度解析:从请求到响应的完美掌控

Spring MVC 核心流程深度解析&#xff1a;从请求到响应的完美掌控 Spring MVC 是 Spring 框架中处理 Web 请求的核心模块&#xff0c;基于 Model-View-Controller (MVC) 设计模式。它以 DispatcherServlet 为入口&#xff0c;实现从 HTTP 请求到响应的完整生命周期管理。下面从…

作者头像 李华
网站建设 2026/5/10 14:15:01

Java毕设选题推荐:基于springboot的游戏售卖商城系统基于SpringBoot+Vue的游戏装备交易商城系统【附源码、mysql、文档、调试+代码讲解+全bao等】

博主介绍&#xff1a;✌️码农一枚 &#xff0c;专注于大学生项目实战开发、讲解和毕业&#x1f6a2;文撰写修改等。全栈领域优质创作者&#xff0c;博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围&#xff1a;&am…

作者头像 李华
网站建设 2026/5/9 12:53:18

【时间之外】AI招聘这么干行不行?

目录 一、背景 二、目标 三、策略 1. 岗位与画像 2. 多元化渠道 3. 构建AI评估体系 4. 实施AI招聘流程 5. 持续优化 四、实施步骤 1. 准备 2. 策略 3. 执行 4. 优化 五、案例 六、总结与展望 一、背景 随着人工智能&#xff08;AI&#xff09;技术的飞速发展&am…

作者头像 李华