返回介绍

solution / 2800-2899 / 2849.Determine if a Cell Is Reachable at a Given Time / README_EN

发布于 2024-06-17 01:02:59 字数 4161 浏览 0 评论 0 收藏 0

2849. Determine if a Cell Is Reachable at a Given Time

中文文档

Description

You are given four integers sx, sy, fx, fy, and a non-negative integer t.

In an infinite 2D grid, you start at the cell (sx, sy). Each second, you must move to any of its adjacent cells.

Return true _if you can reach cell _(fx, fy) _after exactly_ t _seconds_, _or_ false _otherwise_.

A cell's adjacent cells are the 8 cells around it that share at least one corner with it. You can visit the same cell several times.

 

Example 1:

Input: sx = 2, sy = 4, fx = 7, fy = 7, t = 6
Output: true
Explanation: Starting at cell (2, 4), we can reach cell (7, 7) in exactly 6 seconds by going through the cells depicted in the picture above. 

Example 2:

Input: sx = 3, sy = 1, fx = 7, fy = 3, t = 3
Output: false
Explanation: Starting at cell (3, 1), it takes at least 4 seconds to reach cell (7, 3) by going through the cells depicted in the picture above. Hence, we cannot reach cell (7, 3) at the third second.

 

Constraints:

  • 1 <= sx, sy, fx, fy <= 109
  • 0 <= t <= 109

Solutions

Solution 1

class Solution:
  def isReachableAtTime(self, sx: int, sy: int, fx: int, fy: int, t: int) -> bool:
    if sx == fx and sy == fy:
      return t != 1
    dx = abs(sx - fx)
    dy = abs(sy - fy)
    return max(dx, dy) <= t
class Solution {
  public boolean isReachableAtTime(int sx, int sy, int fx, int fy, int t) {
    if (sx == fx && sy == fy) {
      return t != 1;
    }
    int dx = Math.abs(sx - fx);
    int dy = Math.abs(sy - fy);
    return Math.max(dx, dy) <= t;
  }
}
class Solution {
public:
  bool isReachableAtTime(int sx, int sy, int fx, int fy, int t) {
    if (sx == fx && sy == fy) {
      return t != 1;
    }
    int dx = abs(fx - sx), dy = abs(fy - sy);
    return max(dx, dy) <= t;
  }
};
func isReachableAtTime(sx int, sy int, fx int, fy int, t int) bool {
  if sx == fx && sy == fy {
    return t != 1
  }
  dx := abs(sx - fx)
  dy := abs(sy - fy)
  return max(dx, dy) <= t
}

func abs(x int) int {
  if x < 0 {
    return -x
  }
  return x
}
function isReachableAtTime(sx: number, sy: number, fx: number, fy: number, t: number): boolean {
  if (sx === fx && sy === fy) {
    return t !== 1;
  }
  const dx = Math.abs(sx - fx);
  const dy = Math.abs(sy - fy);
  return Math.max(dx, dy) <= t;
}
public class Solution {
  public bool IsReachableAtTime(int sx, int sy, int fx, int fy, int t) {
    if (sx == fx && sy == fy)
      return t != 1;
    return Math.Max(Math.Abs(sx - fx), Math.Abs(sy - fy)) <= t;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文