简体   繁体   English

在采访中设计简单的贪婪蛇游戏的数据结构

[英]Design data structure for simple greedy snake game during interview

This is not a practical question, I just want to discuss and learn data structure design here, I heard it's asked by Google during onsite interview. 这不是一个实际的问题,我只是想在这里讨论和学习数据结构设计,我听说Google在现场采访时会问这个问题。 Please advise me how to improve my design, thanks! 请告诉我如何改进我的设计,谢谢!

At the beginning I wanted to use a deque to store pairs of x,y coordinates for snake's body parts. 一开始我想用deque来存储蛇身体部位的x,y坐标对。

deque<pair<x, y>> snakeBodyParts;

Because it's very easy to push front when snake move - create new coordinate pair as the new head base on old head position and current direction, then pop back to remove the tail. 因为当蛇移动时很容易向前推 - 在新的头部位置和当前方向上创建新的坐标对作为新的头部基础,然后弹回以移除尾部。 In this way move, eat, check head hit wall are all O(1) operations and easy to implement with deque. 通过这种方式移动,吃饭,检查头撞墙都是O(1)操作并且易于用deque实现。 But checking if new head position overlap with snakes body will require looping through all body parts' position - O(L) time complexity, L is number of body parts. 但是检查新的头部位置是否与蛇体重叠将需要遍历所有身体部位的位置 - O(L)时间复杂度,L是身体部位的数量。

To improve it, I thought about putting all coordinates into an unordered_set(C++) or hashset(Java) while still keeping my old deque, it can give me O(1) for checking if head hits body now. 为了改进它,我考虑将所有坐标放入unordered_set(C ++)或hashset(Java),同时仍然保留我的旧deque,它可以给我O(1)来检查头部现在是否击中了身体。 But I don't know if it's a good idea because it almost doubles the memory and amount of codes, whenever I add/remove to deque, I need to do it to my hashset. 但我不知道这是不是一个好主意,因为它几乎使代码的内存和数量增加了一倍,每当我添加/删除deque时,我都需要对我的hashset执行此操作。

unordered_set<pair<int, int>, pair_hash> bodyPartsSet;

I also thought about creating my own structure which is like linkedlist, except it points to previous node: 我还想过创建自己的结构,就像链表一样,除了它指向前一个节点:

SnakeBodyNode {
    int x;
    int y;
    SnakeBodyNode * prev;
}

Then I also need two pointers pointing at head and tail as well as a direction variable. 然后我还需要两个指向头部和尾部的指针以及一个方向变量。

SnakeBodyNode * head;
SnakeBodyNode * tail;
char dir;

However I don't see any advantage of this, still need to hash to get O(1) for checking if head hits body.. 但是我没有看到任何这方面的优点,仍然需要哈希来获取O(1)来检查头是否击中了身体..

Is there any flaw in my deque + hash design or any one have better idea to share? 我的deque + hash设计中是否有任何缺陷或者任何人有更好的想法分享?

I would just use an unordered_set. 我只想使用unordered_set。 Your concerns are: 您的顾虑是:

  • Fast insert of new head - this is O(1) for unordered_set. 快速插入新头 - 这对于unordered_set是O(1)。
  • Fast deletion of existing tail - this is O(1) for unordered_set. 快速删除现有尾部 - 对于unordered_set,这是O(1)。
  • No duplicates (checking that head intersects body) - this is guaranteed for unordered_set (it does not allow duplicates). 没有重复(检查头部与身体相交) - 这对于unordered_set是保证的(它不允许重复)。

When inserting a new head, you don't have to do anything special to check that it intersects with the body; 插入新头时,您无需做任何特殊操作来检查它是否与身体相交; you'll get an error if it does. 如果有错误,你会收到错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM