返回介绍

solution / 1900-1999 / 1958.Check if Move is Legal / README_EN

发布于 2024-06-17 01:03:12 字数 7860 浏览 0 评论 0 收藏 0

1958. Check if Move is Legal

中文文档

Description

You are given a 0-indexed 8 x 8 grid board, where board[r][c] represents the cell (r, c) on a game board. On the board, free cells are represented by '.', white cells are represented by 'W', and black cells are represented by 'B'.

Each move in this game consists of choosing a free cell and changing it to the color you are playing as (either white or black). However, a move is only legal if, after changing it, the cell becomes the endpoint of a good line (horizontal, vertical, or diagonal).

A good line is a line of three or more cells (including the endpoints) where the endpoints of the line are one color, and the remaining cells in the middle are the opposite color (no cells in the line are free). You can find examples for good lines in the figure below:

Given two integers rMove and cMove and a character color representing the color you are playing as (white or black), return true _if changing cell _(rMove, cMove) _to color_ color _is a legal move, or _false_ if it is not legal_.

 

Example 1:

Input: board = [[".",".",".","B",".",".",".","."],[".",".",".","W",".",".",".","."],[".",".",".","W",".",".",".","."],[".",".",".","W",".",".",".","."],["W","B","B",".","W","W","W","B"],[".",".",".","B",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","W",".",".",".","."]], rMove = 4, cMove = 3, color = "B"
Output: true
Explanation: '.', 'W', and 'B' are represented by the colors blue, white, and black respectively, and cell (rMove, cMove) is marked with an 'X'.
The two good lines with the chosen cell as an endpoint are annotated above with the red rectangles.

Example 2:

Input: board = [[".",".",".",".",".",".",".","."],[".","B",".",".","W",".",".","."],[".",".","W",".",".",".",".","."],[".",".",".","W","B",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".","B","W",".","."],[".",".",".",".",".",".","W","."],[".",".",".",".",".",".",".","B"]], rMove = 4, cMove = 4, color = "W"
Output: false
Explanation: While there are good lines with the chosen cell as a middle cell, there are no good lines with the chosen cell as an endpoint.

 

Constraints:

  • board.length == board[r].length == 8
  • 0 <= rMove, cMove < 8
  • board[rMove][cMove] == '.'
  • color is either 'B' or 'W'.

Solutions

Solution 1

class Solution:
  def checkMove(
    self, board: List[List[str]], rMove: int, cMove: int, color: str
  ) -> bool:
    dirs = [(1, 0), (0, 1), (-1, 0), (0, -1), (1, 1), (1, -1), (-1, 1), (-1, -1)]
    n = 8
    for a, b in dirs:
      i, j = rMove, cMove
      t = 0
      while 0 <= i + a < n and 0 <= j + b < n:
        t += 1
        i, j = i + a, j + b
        if board[i][j] in ['.', color]:
          break
      if board[i][j] == color and t > 1:
        return True
    return False
class Solution {
  private static final int[][] DIRS
    = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
  private static final int N = 8;

  public boolean checkMove(char[][] board, int rMove, int cMove, char color) {
    for (int[] d : DIRS) {
      int i = rMove, j = cMove;
      int t = 0;
      int a = d[0], b = d[1];
      while (0 <= i + a && i + a < N && 0 <= j + b && j + b < N) {
        ++t;
        i += a;
        j += b;
        if (board[i][j] == '.' || board[i][j] == color) {
          break;
        }
      }
      if (board[i][j] == color && t > 1) {
        return true;
      }
    }
    return false;
  }
}
class Solution {
public:
  vector<vector<int>> dirs = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
  int n = 8;

  bool checkMove(vector<vector<char>>& board, int rMove, int cMove, char color) {
    for (auto& d : dirs) {
      int a = d[0], b = d[1];
      int i = rMove, j = cMove;
      int t = 0;
      while (0 <= i + a && i + a < n && 0 <= j + b && j + b < n) {
        ++t;
        i += a;
        j += b;
        if (board[i][j] == '.' || board[i][j] == color) break;
      }
      if (board[i][j] == color && t > 1) return true;
    }
    return false;
  }
};
func checkMove(board [][]byte, rMove int, cMove int, color byte) bool {
  dirs := [8][2]int{{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}
  n := 8
  for _, d := range dirs {
    a, b := d[0], d[1]
    i, j := rMove, cMove
    t := 0
    for 0 <= i+a && i+a < n && 0 <= j+b && j+b < n {
      t++
      i += a
      j += b
      if board[i][j] == '.' || board[i][j] == color {
        break
      }
    }
    if board[i][j] == color && t > 1 {
      return true
    }
  }
  return false
}

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

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

发布评论

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