查看: 1133| 回复: 0
跳转到指定楼层
上一主题 下一主题
收起左侧

[Leetcode] 353 Design snake game 总是过不了

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
不明白x、y坐标为啥更新的不对,test case总是过不了。劳烦大神指正
  1. class SnakeGame {

  2.     /** Initialize your data structure here.
  3.         @param width - screen width
  4.         @param height - screen height
  5.         @param food - A list of food positions
  6.         E.g food = [[1,1], [1,0]] means the first food is positioned at [1,1], the second is at [1,0]. */
  7.     int width;
  8.     int height;
  9.     int[][] food;
  10.     int foodIndex;
  11.     Deque<int[]> queue;
  12.     public SnakeGame(int width, int height, int[][] food) {
  13.         this.width = width;
  14.         this.food = food;
  15.         foodIndex = 0;
  16.         queue = new ArrayDeque<>();
  17.         queue.addLast(new int[]{0, 0});
  18.     }  
  19.    
  20.     /** Moves the snake.
  21.         @param direction - 'U' = Up, 'L' = Left, 'R' = Right, 'D' = Down
  22.         [url=home.php?mod=space&uid=160137]@return[/url] The game's score after the move. Return -1 if game over.
  23.         Game over when snake crosses the screen boundary or bites its body. */
  24.     public int move(String direction) {
  25.         int[] curPos = queue.peekFirst();
  26.         int curX = curPos[0];
  27.         int curY = curPos[1];
  28.         
  29.         if (direction.equals("U")) {
  30.             --curX;
  31.         } else if (direction.equals("D")) {
  32.             ++curX;
  33.         } else if (direction.equals("L")) {
  34.             --curY;
  35.         } else if (direction.equals("R")) {
  36.             ++curY;
  37.         }
  38.    
  39.         if (outOfRange(curX, curY) || isTouchBody(curX, curY)) {
  40.             return -1;
  41.         } else if (foodIndex < food.length && curX == food[foodIndex][0] && curY == food[foodIndex][1]) {
  42.             foodIndex++;
  43.             queue.addFirst(new int[]{curX, curY}); //头部增长
  44.         } else {
  45.             queue.removeLast();
  46.             queue.addFirst(new int[]{curX, curY});//单纯前进
  47.         }
  48.         return queue.size() - 1;
  49.     }
  50.    
  51.     boolean outOfRange (int x, int y) {
  52.         return x < 0 || x >= height || y < 0 || y >= width;
  53.     }
  54.    
  55.     boolean isTouchBody (int x, int y) {
  56.         for (int[] pos : queue) {
  57.             if (x == pos[0] && y == pos[1]) {
  58.                 if (x != queue.peekLast()[0] || y != queue.peekLast()[1]) {
  59.                     return true;
  60.                 }
  61.             }
  62.         }
  63.         return false;
  64.     }
  65. }
复制代码



补充内容 (2020-9-22 12:23):
搞清楚哪错了,constructor里没传height……

上一篇:OA 遇到的一道题 请大佬帮忙看看
下一篇:求指导,duplicate subarray(一道codesignal的OA题)
您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表